I have a dataframe, df, where I would like to perform an addition based on values in a different column. The addition calculation is based on the id value.
Data
id date value start status
aa Q1.22 hello 80
aa Q2.22 hi -200 2000
aa Q3.22 hey -400
aa Q4.22 sure -500
bb Q3.23 ok 30
bb Q4.23 sure 10 100
Desired
id date value start status update
aa Q1.22 hello 80
aa Q2.22 hi -200 2000 1800
aa Q3.22 hey -400 1400
aa Q4.22 sure -500 900
bb Q3.23 ok 30
bb Q4.23 sure 10 100 110
Logic
The addition will begin at the first display of a single value. For example we see that the number 2000 is a single non consecutive value. The start point of the addition calculation will begin here. Similar to the 100 value (it is an ending value)
We are just adding the values in the 'start' column to the value in the 'status' column. The adding ends with the id. The adding calculation is grouped by id.
Doing
df = df.groupby(["id"]).sum()
df['update'] = df.groupby(["id"]).sum()['start']
However,
I am still researching this, any suggestion is appreciated.