1

I read this post but it I couldn`t understand it: Python pandas cumsum with reset everytime there is a 0

This is my dataframe:

df = pd.DataFrame({'pct':range(10, 20), 'day':[0,1,2,3,4,5,0,1,4,5] })

I want to add another column to the df that shows the cumsum of pct and each time day is 5, cumsum resets. This is my desired outcome:

   day  pct  result
0    0   10      10
1    1   11      21
2    2   12      33
3    3   13      46
4    4   14      60
5    5   15      75
6    0   16      16 
7    1   17      33 
8    4   18      51
9    5   19      70
Amir
  • 978
  • 1
  • 9
  • 26

2 Answers2

3

Here's one approach using a custom grouper and taking the cumsum along the groups:

g = df.day.eq(5).shift(1).cumsum().fillna(0)
df['result'] = df.groupby(g).pct.cumsum()

   pct  day  result
0   10    0      10
1   11    1      21
2   12    2      33
3   13    3      46
4   14    4      60
5   15    5      75
6   16    0      16
7   17    1      33
8   18    4      51
9   19    5      70
yatu
  • 86,083
  • 12
  • 84
  • 139
3

Use:

df['result']=df.groupby(df.day.eq(5).shift().fillna(0).cumsum())['pct'].cumsum()
print(df)

   pct  day  result
0   10    0      10
1   11    1      21
2   12    2      33
3   13    3      46
4   14    4      60
5   15    5      75
6   16    0      16
7   17    1      33
8   18    4      51
9   19    5      70
anky
  • 74,114
  • 11
  • 41
  • 70