I have the following dataframe:
pct_day True_False
Date
2018-01-02 NaN False
2018-01-03 0.006399 False
2018-01-04 0.004029 False
2018-01-05 0.007034 False
2018-01-08 0.001662 False
... ... ...
2020-01-23 0.001141 True
2020-01-24 -0.009042 True
2020-01-27 -0.015731 True
2020-01-28 0.010054 True
2020-01-29 -0.000867 False
522 rows × 2 columns
What i want to do is cumsum()
on False
and restart when it toggles to True
. I want the cumulative sum to be only on consecutive True or False.
A similar question was asked here: How to groupby consecutive values in pandas DataFrame
for row in data3:
if data3.True_False == data3.True_False.shift():
print(data3.pct_day.cumsum())
But this produces an error:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
How can i get the iteration to continue only if the condition is true?