I have this dataframe -
counter duration amount
0 1 0.08 1,235
1 2 0.36 1,170
2 3 1.04 1,222
3 4 0.81 1,207
4 5 3.99 1,109
5 6 1.20 1,261
6 7 4.24 1,068
7 8 3.07 1,098
8 9 2.08 1,215
9 10 4.09 1,043
10 11 2.95 1,176
11 12 3.96 1,038
12 13 3.95 1,119
13 14 3.92 1,074
14 15 3.91 1,076
15 16 1.50 1,224
16 17 3.65 962
17 18 3.85 1,039
18 19 3.82 1,062
19 20 3.34 917
I would like to create another column based on the following logic:
For each row, I want to calculate a running sum of 'duration' but it should be a running sum for the rows that are below the current row (lead and not lag). I would like to stop the calculation when the running sum reaches 5 -> when it reaches 5, I want to return the running sum for 'amount' (with the same logic).
For instance, for 'counter' 1 it should take the first 4 rows (0.08+0.36+1.04+0.81<5) and then to return 1,235+1,170+1,222+1,207=4834
for 'counter' 2 it should take only 0.36 + 1.04 + 0.81<5 and to return 1,170+1,222+1,207=3599
Will appreciate any help!