Apr-30-2020, 07:49 PM
I'm trying to get a cumulative total if the code is equal to 100.
df = pd.DataFrame(data = {'Code':[100,100,200,300,100], 'Value':[1000,1000,500,750,1000], 'Expected_Cum_100':[1000,2000,2000,2000,3000],})This was my try, but I couldn't get the expected result
c = df["Value"].cumsum() df['Cum_100'] = c.mask(df["Code"] != 100).ffill()
Output:df
Out[81]:
Code Expected_Cum_100 Value Cum_100
0 100 1000 1000 1000.0
1 100 2000 1000 2000.0
2 200 2000 500 2000.0
3 300 2000 750 2000.0
4 100 3000 1000 4250.0
Appreciate it if anyone can suggest a way to get the expected cumulative total.