2 回答

TA貢獻1846條經(jīng)驗 獲得超7個贊
df['valuedif']=df[['LOCATION', 'TIME', 'Value']].groupby('LOCATION').Value.apply(lambda x: x.diff())
print(df)
LOCATION TIME Value valuedif
0 AUS 2000 33.595673 NaN
1 AUS 2001 57.862362 24.266689
2 AUS 2002 58.588608 0.726246
3 UK 2000 61.700000 NaN
4 UK 2001 63.243232 1.543232
5 UK 2002 66.235122 2.991890

TA貢獻1830條經(jīng)驗 獲得超3個贊
如果我正確理解你在尋找什么,解決方案是:
df.groupby("LOCATION").diff()
輸出是:
TIME Value
0 NaN NaN
1 1.0 24.266689
2 1.0 0.726246
3 NaN NaN
4 1.0 1.543232
5 1.0 2.991890
添加回答
舉報