创建一列,显示上一期间的+/-

时间:2019-08-27 02:34:15

标签: python pandas group-by percentage

我有此数据框,我想创建一列以显示从期间到当前期间的金额变化百分比,同时按Company_Id,国家/地区和期间分组。

Company_Id  Country     Period         Amount
MOO17       USA       Previous Period   500
KQR20       UK        Previous Period   1000    
KQR20       UK        Current Period    20000
ABY88       Ireland    Previous Period  1000
ABY88       Ireland    Current Period   250 
SOQ99       Japan      Previous Period  8000
SOQ99       Japan      Current Period   25000
RTU89       China      Current Period   20000
RTU89       China      Previous Period  1000
WER67       Canada     Current Period   5000
WER67       Canada     Previous Period  20000

我尝试了以下方法:

df['desired']= df['Amount'] / df.groupby(['Company_Id','Country','Period])['Amount'].shift(1)

df= df.sort_values(by=['Company_Id','Country','Period],ascending=[True, True, False],inplace=True)
df['desired'] = df.groupby(['Company_Id','Country','Period])['Amount].pct_change()

我不断得到与所需分组不符的nan或值。

所需的输出:

Company_Id  Country     Period         Amount     Desired
MOO17       USA       Previous Period   500        na
KQR20       UK        Previous Period   1000       na
KQR20       UK        Current Period    20000      1900%
ABY88       Ireland    Previous Period  1000       na
ABY88       Ireland    Current Period   250       -75%
SOQ99       Japan      Previous Period  8000      na
SOQ99       Japan      Current Period   25000    212.5%
RTU89       China      Current Period   20000    na
RTU89       China      Previous Period  1000     -95%
WER67       Canada     Current Period   5000     na
WER67       Canada     Previous Period  20000   300%

1 个答案:

答案 0 :(得分:1)

df2['desired'] = df2.groupby(['Company_Id','Country'])['Amount'].pct_change()*100

如果要添加百分比符号,可以执行以下操作。但是,它将把列的数据类型从object更改为float64

df2['desired'] = (df2.groupby(['Company_Id','Country'])['Amount'].pct_change()*100).astype(str) + '%'

enter image description here