I would like to calculate the average score of every group in a column weighted by another column.
I will write an example to clarify my goal. Let's say I have the following pandas dataframe:
Group | # items | score |
---|---|---|
A | 10 | 2 |
A | 15 | 4 |
A | 20 | 6 |
B | 5 | 5 |
B | 10 | 8 |
My desired output would be:
Group | avg_weighted_score |
---|---|
A | 4.444 |
B | 7 |
df = pd.DataFrame([['A',10,2],['A',15,4],['A',20,6],['B',5,5],['B',10,8]],columns = ['Group', '#items', 'score'])