Looking at the table, I am looking to replicate the Rollover column in a Python dataframe. The Rollover looks at:
Rollover is created when production > max capacity. The following date will look at the previous day and will either add the delta of production – max capacity or will subtract the delta of max capacity – production.
Rollover will never be less than 0.
2
You can identify the index of the first time production > max_capacity
, set all previous entries equal to 0, then perform a cumulative sum.
df['EXTRA'] = df['PRODUCTION'] - df['MAX_CAPACITY']
first_pos_idx = df['EXTRA'].gt(0).idxmax()
df.loc[:first_pos_idx-1, "EXTRA"] = 0
df['ROLLOVER'] = df['EXTRA'].cumsum()
Result:
DATE PRODUCTION MAX_CAPACITY EXTRA ROLLOVER
0 2024-01-01 10 11 0 0
1 2024-01-02 8 11 0 0
2 2024-01-03 8 11 0 0
3 2024-01-04 15 11 4 4
4 2024-01-05 15 11 4 8
5 2024-01-06 12 11 1 9
6 2024-01-07 10 11 -1 8
7 2024-01-08 9 11 -2 6
8 2024-01-09 14 11 3 9
9 2024-01-10 16 11 5 14
10 2024-01-11 2 11 -9 5