user11321985
user11321985

Reputation:

Sum up value if greater then previous value

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

Answers (1)

Allen Qin
Allen Qin

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

Related Questions