I have following dataframe in which I want to make a cumulative sum on a certain column (Value
), together with a group by on field Group
, but reset that cumulative sum to 0 when a value in another column (Quantity
) is 0.
Group | Quantity | Value | Cumulative_sum |
---|---|---|---|
A | 10 | 200 | 200 |
B | 5 | 300 | 300 |
A | 1 | 50 | 250 |
A | 0 | 100 | 0 |
C | 5 | 400 | 400 |
A | 10 | 300 | 300 |
B | 10 | 200 | 500 |
A | 15 | 350 | 650 |
I have tried working with blocks like in the code below, but the issue is that it doesn't do the cumulative sum correctly:
blocks = df['Quantity'].eq(0)[::-1].cumsum()[::-1]
df['temp_field'] = (df.groupby(['Group', blocks])
['Value'].cumsum()
.where(df['Quantity']!=0,df['Value'])
)
df['Cumulative_sum'] = np.where(df['Quantity'] == 0, 0, df['temp_field'])
Could anyone help with this?