I am trying to find the cumulative sum for four consecutive rows in a dataframe based on a condition.
The new column ('veh_time_TOT') is a sum of four consecutive 'veh_time(s)' values and the condition is 'Day_type': Weekend or Weekday.
Here is how the data is now set up:
veh-time(s) distance(m) Day_type
0 72 379.0 Weekday
1 70 379.0 Weekday
2 50 379.0 Weekday
3 60 379.0 Weekday
4 70 379.0 Weekday
5 65 379.0 Weekday
6 30 379.0 Weekend
7 35 379.0 Weekend
8 30 379.0 Weekend
9 30 379.0 Weekend
10 20 379.0 Weekend
Here is the desired output:
veh-time(s) distance(m) Day_type veh_time_TOT
0 72 379.0 Weekday 0
1 70 379.0 Weekday 0
2 50 379.0 Weekday 0
3 60 379.0 Weekday 252
4 70 379.0 Weekday 250
5 65 379.0 Weekday 245
6 30 379.0 Weekend 0
7 35 379.0 Weekend 0
8 30 379.0 Weekend 0
9 30 379.0 Weekend 125
10 20 379.0 Weekend 115
I've tried several things but the only thing I could find is using the .cumsum function which only finds the sum for 2 consecutive rows. The zeros in the "veh_time_TOT" are there because there haven't been 4 rows yet to make up the sum.
My thinking that this would be a combination of .cumsum and conditional if statement that goes on a loop.
What do you guys think? Any help is appreciated.