I have 2 dataframes - One with the values under each group. The second dataframe has weights for each group to get the weighted average.
df1 =
product group1 group2 group3 group4 group5
xyz 223056 907858 4803 586623 835423
abc 557042 382306 915624 786236 413817
pqr 951348 723503 587397 141975 567826
df2 =
group weight
group1 0.5
group2 7.5
group3 10
group4 1.5
group5 50
I'd like to get a weighted average of all groups for each product and append it to df1. For example :
product group1 group2 group3 group4 group5 weighted-average
xyz 223056 907858 4803 586623 835423 40
abc 557042 382306 915624 786236 413817 25
pqr 951348 723503 587397 141975 567826 30
Is there an easy way to do this? I managed to do this in excel, but I would like to try it in pandas as I'm learning it still.
Use
np.averagewithDataFrame.reindexfor same order columns and index:If always same order is possible use: