I have a dataset, df, where I would like to:
- Take the average of the TotalB column based upon grouping the TotalB column.
- I would then like to take this new column and subtract the free value to obtain the Used value
df1
date name rack TotalB freeB
11/20/2020 a yes 11 5
11/20/2020 a yes 10 5
11/20/2020 a yes 12 5
11/20/2020 a yes 10 5
11/20/2020 b no 5 2
11/20/2020 b no 5 2
11/20/2020 b no 6 2
Desired Outcome
date name rack TotalB freeB UsedB
11/20/2020 a yes 10.75 5 5.75
11/20/2020 b no 5.33 2 3.33
What I am doing:
df.groupby('rack')['TotalB'].mean()
UsedB = df["TotalB"] - df["freeB"]
I am having trouble with retaining all the columns within the dataset. Any suggestion is appreciated.