I have a pandas dataframe like below for the columns value_to_sum and indicator. I'd like to sum all values within value_to_sum up to and including the most recent value within that column where indicator == True. If indicator == False, I do not want to sum.
| row | value_to_sum | indicator | desired_outcome |
|---|---|---|---|
| 1 | 1 | True | NaN |
| 2 | 3 | True | 1 |
| 3 | 1 | False | NaN |
| 4 | 2 | False | NaN |
| 5 | 4 | False | NaN |
| 6 | 6 | True | 10 |
| 7 | 2 | True | 6 |
| 8 | 3 | False | NaN |
How can I achieve the values under desired_outcome?
value_to_sumon that same row within the sum underdesired_outcome. But the sum should be inclusive of the last row whereindicator == True. So "most recent" means the most recent row previous to the row we're at.