我试图根据ID和一个月累积数据使用情况,以便将累积数据添加为附加列。
这是我的DF样本的代码:
df = pd.DataFrame({'ID':["SAD1", "SAD2", "SAD1", "SAD2", "SAD1", "SAD2", "SAD3"],
'Month':["201701","201701","201702","201702","201703","201703", "201703"],
'Usage':[20,40,100,50, 30, 30, 2000]})
我的数据帧看起来像这样:
ID Month Usage
0 SAD1 201701 20
1 SAD2 201701 40
2 SAD1 201702 100
3 SAD2 201702 50
4 SAD1 201703 30
5 SAD2 201703 30
6 SAD3 201703 2000
我想最终得到的结果是:
ID Month Usage Cum Usage
0 SAD1 201701 20 20
1 SAD2 201701 40 40
2 SAD1 201702 100 120
3 SAD2 201702 50 90
4 SAD1 201703 30 150
5 SAD2 201703 30 120
6 SAD3 201703 2000 2000
谁能帮助我如何将其应用于我的DF?
groupby
和 cumsum
df['Cum Usage'] = df.groupby('ID').cumsum()