ala*_*ant 5 python dataframe pandas
我在代码中使用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 '')
)
)
Run Code Online (Sandbox Code Playgroud)
输出是:
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%
Run Code Online (Sandbox Code Playgroud)
这就是我所追求的,但我收到了折旧警告,
FutureWarning: The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead.table.append()
Run Code Online (Sandbox Code Playgroud)
我更改了代码以使用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 '')
)],
)
Run Code Online (Sandbox Code Playgroud)
现在我得到:
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%
Run Code Online (Sandbox Code Playgroud)
这不是我所期望的 - 请注意与追加输出相比的百分比变化(20%和83.3%)。任何意见将不胜感激。
pd.concat像这样使用。将内部命令转换为dfusing 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%
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7669 次 |
| 最近记录: |