I'm using an existing solution to try to produce a cumulative sum that resets after a certain value (in this case >= 16). Currently I get the following output, but there are cases when the cumsum is still greater than 16.
Size cumsum
8 8
8 16 ---correct
8 8
8 16 ---correct
7 7
6 13 (should be reset here since next value causes cumsum >16)
7 20 ---incorrect
6 6
5 11
2 13
The code I am using is:
df = pd.DataFrame({'Size':[8,8,8,8,7,6,7,6,5,2]})
ls = []
cumsum = 0
last_reset = 0
for _, row in df.iterrows():
cumsum = cumsum + row.Size
ls.append(cumsum)
if cumsum >= 16:
last_reset = cumsum
cumsum = 0
df['cumsum'] = ls
Any ideas how to correct this?