Search code examples
pythonpandascumsum

Cumulative sum with a reset


I want to use the cumsum function of pandas and reset this cumulative series when a condition is fulfilled. For example i have this df :

       o  values
   0   1       4
   1   1       4
   2   2       2
   3   2       5
   4   3       1
   5   3      10

and where the value of 'o' is +1 i want to reset the cumulative sum. I know I can find the condition with :

s = df['o'].diff() == 1

which return a boolean series where a row ['o'] is + 1.

How can i continue to have the following result :

       o  values  cum_sum
   0   1       4        4
   1   1       4        8
   2   2       2        2
   3   2       5        7
   4   3       1        1
   5   3      10       11

Thank for your help and your time !


Solution

  • You can use pd.DataFrame.groupby and cumsum():

    df['cumsum']=df.groupby('o').cumsum()
    

    Output:

    df
       o  values  cumsum
    0  1       4       4
    1  1       4       8
    2  2       2       2
    3  2       5       7
    4  3       1       1
    5  3      10      11