I've come across a bunch of other weighted mean pandas questions but none of them seem to do what I'm trying to do. I have the following df:
Primary_Key Team Quantity Value 1 Value 2
0 A Blue 10 20 10
1 B Red 5 19 30
2 C Green 8 13 29
3 D Blue 12 24 18
4 E Red 15 25 19
5 F Green 12 18 23
I'm trying to calculate the weighted average of each of the values for each team, so I'd get the following result_df:
Team Quantity Value 1 Value 2
0 Blue 10 20*10/22 10*10/22
1 Red 5 19*5/20 30*5/20
2 Green 8 13*8/20 29*8/20
3 Blue 12 24*12/22 18*12/22
4 Red 15 25*15/20 19*15/20
5 Green 12 18*12/20 23*12/20
where each entry under the Value columns have had the following calculation done on them:
weighted_mean = value * (quantity/team's total quantity)
I'm thinking I'd have to use the .apply(lambda x:...) function somehow but I don't know how I would easily get the values for the team's total quantity. I also came across the numpy.average function but I don't think it would be useful here.
Any help would be much appreciated!