Look up weighting dataframe and apply to a column

118 views Asked by At

I have two dataframes:

df1:

    Variable Name      Weight
     Variable1           2
     Variable2           .5 

df2:

    Variable1      Variable2
        3              4 
        2              5 

How can I multiply the weights by the numbers in each column to get the result:

df3:

    Variable1      Variable2
        6              2 
        4              2.5 
1

There are 1 answers

0
jpp On BEST ANSWER

You can multiply a dataframe by a series or mapped index:

s = df1.set_index('Variable Name')['Weight']

df3 = df2 * s   # or df2.columns.map(s.get)

print(df3)

   Variable1  Variable2
0        6.0        2.0
1        4.0        2.5