计算平面表格的百分比变化
Calculate % change in flat tables
我想从 df1 计算百分比变化,它应该给出 df2。你能帮帮我吗?
df1
lst=[['01012021','A',10],['01012021','B',20],['01012021','A',12],['01012021','B',23]]
df1=pd.DataFrame(lst,columns=['Date','FN','AuM'])
df2
lst=[['01012021','A',10,''],['01012021','B',20,''],['01012021','A',12,0.2],['01012021','B',23,0.15]]
df2=pd.DataFrame(lst,columns=['Date','FN','AuM','%_delta'])
谢谢
使用groupby
和pct_change
:
df1['%_delta'] = df1.groupby('FN')['AuM'].pct_change()
print(df1)
# Output:
Date FN AuM %_delta
0 01012021 A 10 NaN
1 01012021 B 20 NaN
2 01012021 A 12 0.20
3 01012021 B 23 0.15
我想从 df1 计算百分比变化,它应该给出 df2。你能帮帮我吗?
df1
lst=[['01012021','A',10],['01012021','B',20],['01012021','A',12],['01012021','B',23]]
df1=pd.DataFrame(lst,columns=['Date','FN','AuM'])
df2
lst=[['01012021','A',10,''],['01012021','B',20,''],['01012021','A',12,0.2],['01012021','B',23,0.15]]
df2=pd.DataFrame(lst,columns=['Date','FN','AuM','%_delta'])
谢谢
使用groupby
和pct_change
:
df1['%_delta'] = df1.groupby('FN')['AuM'].pct_change()
print(df1)
# Output:
Date FN AuM %_delta
0 01012021 A 10 NaN
1 01012021 B 20 NaN
2 01012021 A 12 0.20
3 01012021 B 23 0.15