How to calculate an an accumulated value conditionally

Question:

I have the following pandas dataframe:

diff_hours   stage    sensor
0            0        20
0            0        21
0            0        21
1            0        22
5            0        21
0            0        22
0            1        20
7            1        23
0            1        24
0            3        25
0            3        28
6            0        21
0            0        22

I need to calculated an accumulated value of diff_hours while stage is growing. When stage drops to 0, the accumukated value of diff_hours should restart.

This is the expected result:

acc_hours   stage    sensor
0            0        20
0            0        21
0            0        21
1            0        22
6            0        21
6            0        22
6            1        20
13           1        23
13           1        24
13           3        25
13           3        28
0            0        21
0            0        22
Asked By: Fluxy

||

Answers:

Use cumsum on the negative condition:

blocks = df['stage'].diff().lt(0).cumsum()
df['acc_hours'] = df['diff_hours'].groupby(blocks).cumsum()

Output:

    diff_hours  stage  sensor  acc_hours
0            0      0      20          0
1            0      0      21          0
2            0      0      21          0
3            1      0      22          1
4            5      0      21          6
5            0      0      22          6
6            0      1      20          6
7            7      1      23         13
8            0      1      24         13
9            0      3      25         13
10           0      3      28         13
11           6      0      21          6
12           0      0      22          6
Answered By: Quang Hoang
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.