我有以下内容:
data = pd.DataFrame({'ID' : (1,2,1,2,1,2),
'year': (1,1,2,2,3,3),
'total_change' : (2,1,7,4,14,6)})
我想计算每行的绝对年度变化,因此需要从上一年减去当前年份(对于total_change变量。
我希望输出看起来如下,我将输出列标记为annual_change:
output = pd.DataFrame({'ID' : (1,2,1,2,1,2),
'year': (1,1,2,2,3,3),
'total_change' : (2,1,7,4,14,6),
'annual_change' : (2,1,5,3,7,2)})
答案 0 :(得分:2)
output = data.assign(annual_change=data.groupby("ID")['total_change'].apply(lambda x:x.diff().fillna(x)))
输出:
ID total_change year annual_change
0 1 2 1 2.0
1 2 1 1 1.0
2 1 7 2 5.0
3 2 4 2 3.0
4 1 14 3 7.0
5 2 6 3 2.0