在 pandas 1.4 中使用 pd.concat 而不是 df.append

我在我的代码中使用 df.append() 来附加数据框列之间的百分比变化。随着 df.append() 在 pandas 1.4 中被贬值,我正在尝试使用 pd.concat 但我无法复制输出。

所以这就是我现在所拥有的:

import numpy as np
import pandas as pd

df = pd.DataFrame({"A": ["foo", "foo", "foo", "foo", "foo",
                         "bar", "bar", "bar", "bar"],
                   "B": ["one", "one", "one", "two", "two",
                         "one", "one", "two", "two"],
                   "C": ["small", "large", "large", "small",
                         "small", "large", "small", "small",
                         "large"],
                   "D": [1, 2, 2, 3, 3, 4, 5, 6, 7],
                   "E": [2, 4, 5, 5, 6, 6, 8, 9, 9]})

table = pd.pivot_table(df, values='D', index=['A', 'B'],
                    columns=['C'], aggfunc=np.sum, fill_value=0, margins=True)

table.append(
    (table
     .iloc[-1]
     .pct_change(periods=1, fill_method=None)
     .fillna('')
     .apply(lambda x: '{:.1%}'.format(x) if x else '')
    )
)

输出是:

    C   large   small   All
A   B           
bar one 4   5   9
two 7   6   13
foo one 4   1   5
two 0   6   6
All     15  18  33
            20.0%   83.3%

这就是我所追求的,但我收到了贬值警告,

FutureWarning: The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead.table.append()

我将代码更改为使用 pd.concat() ,如下所示:

pd.concat([table,
    (table
     .iloc[-1]
     .pct_change(periods=1, fill_method=None)
     .fillna('')
     .apply(lambda x: '{:.1%}'.format(x) if x else '')
    )],
)

现在我得到:

    large   small   All 0
(bar, one)  4.0 5.0 9.0 NaN
(bar, two)  7.0 6.0 13.0    NaN
(foo, one)  4.0 1.0 5.0 NaN
(foo, two)  0.0 6.0 6.0 NaN
(All, ) 15.0    18.0    33.0    NaN
large   NaN NaN NaN 
small   NaN NaN NaN 20.0%
All NaN NaN NaN 83.3%

这不是我所期望的 - 请注意与 append 的输出相比的百分比变化(20% 和 83.3%)。任何输入将不胜感激。

stack overflow Using pd.concat instead of df.append with pandas 1.4
原文答案
author avatar

接受的答案

使用 pd.concat 这样。使用 df 将内部命令转换为 Series.to_frame ,然后使用 df.T

In [74]: pd.concat([table, table.iloc[-1].pct_change(periods=1, fill_method=None).fillna('').apply(lambda x: '{:.1%}'.format(x) if x else '').to_frame().T])
Out[74]: 
C       large  small    All
A   B                      
bar one     4      5      9
    two     7      6     13
foo one     4      1      5
    two     0      6      6
All        15     18     33
               20.0%  83.3%

答案: