How can I use cumsum within a group in Pandas? How can I use cumsum within a group in Pandas? python python

How can I use cumsum within a group in Pandas?


You can call transform and pass the cumsum function to add that column to your df:

In [156]:df['cumsum'] = df.groupby('id')['val'].transform(pd.Series.cumsum)dfOut[156]:  id   stuff  val  cumsum0  A      12    1       11  B   23232    2       22  A      13   -3      -23  C    1234    1       14  D    3235    5       55  B    3236    6       86  C  732323   -2      -1

With respect to your error, you can't call cumsum on a Series groupby object, secondly you're passing the name of the column as a list which is meaningless.

So this works:

In [159]:df.groupby('id')['val'].cumsum()Out[159]:0    11    22   -23    14    55    86   -1dtype: int64