Python 查找加权数据帧并应用于列

Python 查找加权数据帧并应用于列,python,pandas,dataframe,weighting,Python,Pandas,Dataframe,Weighting,我有两个数据帧: df1: df2: 如何将权重乘以每列中的数字以得到结果: df3: 可以将数据帧与序列或映射索引相乘: 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 有关更多有用信息,请参阅

我有两个数据帧:

df1:

df2:

如何将权重乘以每列中的数字以得到结果:

df3:


可以将数据帧与序列或映射索引相乘:

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
有关更多有用信息,请参阅。
    Variable1      Variable2
        3              4 
        2              5 
    Variable1      Variable2
        6              2 
        4              2.5 
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