-2

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 !

Pi-R
  • 644
  • 3
  • 10

2 Answers2

0

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
MrNobody33
  • 6,413
  • 7
  • 19
0

use:

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

output:

    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
David Erickson
  • 16,433
  • 2
  • 19
  • 35