Reputation:
I'm trying to keep summing up the values only if previous value is greater than my current value (summed up value). I tried doing cumsum but I'm not able to do it based on a condition.
Upvotes: 0
Views: 56
Reputation: 19957
You can use np.where to determine whether to keep the old value or take the cumsum:
df['finalvalue'] = (
np.where(df.value.gt(df.value.shift())[::-1],
df.value, df.iloc[::-1].value.cumsum()[::-1])
)
Upvotes: 1