I calculate a number of aggregate functions using groupby and agg , because I need different aggregate functions for different variables, e.g. not the sum of all, but sum and mean of x, mean of y, etc.
Is there a way to calculate a weighted average using agg? I have found lots of examples, but none with agg.
I can calculate the weighted average manually, as in the code below (note the lines with **), but I was wondering if there is a more elegant and direct way?
Can I create my own function and use that with agg?
For the sake of clarity, I fully understand there are other solutions, e.g.
Calculate weighted average with pandas dataframe
and lots, lots more. However, as I said, I am not sure how to implement these solutions with an agg, and I need agg because I need to apply different aggregate functions to different columns (again, not the sum of all, but sum and mean of x, mean of y, etc.).
import numpy as np
import pandas as pd
df= pd.DataFrame(np.random.randint(5,8,(1000,4)), columns=['a','b','c','d'])
**df['c * b']= df['c']* df['b']**
g = df.groupby('a').agg(
{'b':['sum', lambda x: x.sum() / df['b'] .sum(), 'mean'],
'c':['sum','mean'], 'd':['sum'],
'c * b':['sum']})
g.columns = g.columns.map('_'.join)
**g['weighted average of c'] = g['c * b_sum'] / g['b_sum']**