查找加权数据框并应用于列
Look up weighting dataframe and apply to a column
我有两个数据框:
df1:
Variable Name Weight
Variable1 2
Variable2 .5
df2:
Variable1 Variable2
3 4
2 5
如何将权重乘以每列中的数字得到结果:
df3:
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
我有两个数据框:
df1:
Variable Name Weight
Variable1 2
Variable2 .5
df2:
Variable1 Variable2
3 4
2 5
如何将权重乘以每列中的数字得到结果:
df3:
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