我有此数据框,我想创建一列以显示从期间到当前期间的金额变化百分比,同时按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%