May-01-2020, 02:21 PM
I managed to do this by creating an intermediary column as a new value.
df['New_Value'] = df.apply(lambda x : x['Value'] if (x['Code'] == 100) else np.nan, axis=1) c = df['New_Value'].cumsum() df['Cum_100'] = c.mask(df["Code"] != 100).ffill() del ['New_Value']
Output:print(df)
Code Expected_Cum_100 Value New_Value Cum_100
0 100 1000 1000 1000.0 1000.0
1 100 2000 1000 1000.0 2000.0
2 200 2000 500 NaN 2000.0
3 300 2000 750 NaN 2000.0
4 100 3000 1000 1000.0 3000.0