简体   繁体   中英

Look up weighting dataframe and apply to a column

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 

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM