我有
df = pd.DataFrame.from_dict({'id': ['A', 'B', 'A', 'C', 'D', 'B', 'C'], 'val': [1,2,-3,1,5,6,-2], 'stuff':['12','23232','13','1234','3235','3236','732323']})
id stuff val
0 A 12 1
1 B 23232 2
2 A 13 -3
3 C 1234 1
4 D 3235 5
5 B 3236 6
6 C 732323 -2
我想val
为 each运行一些id
,所以所需的输出如下所示:
id stuff val cumsum
0 A 12 1 1
1 B 23232 2 2
2 A 13 -3 -2
3 C 1234 1 1
4 D 3235 5 5
5 B 3236 6 8
6 C 732323 -2 -1
这是我尝试过的:
df['cumsum'] = df.groupby('id').cumsum(['val'])
和
df['cumsum'] = df.groupby('id').cumsum(['val'])
这是我得到的错误:
ValueError: Wrong number of items passed 0, placement implies 1