【问题标题】:Look up weighting dataframe and apply to a column查找权重数据框并应用于列
【发布时间】:2018-11-09 17:41:38
【问题描述】:

我有两个数据框:

df1:

    Variable Name      Weight
     Variable1           2
     Variable2           .5 

df2:

    Variable1      Variable2
        3              4 
        2              5 

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

df3:

    Variable1      Variable2
        6              2 
        4              2.5 

【问题讨论】:

标签: python pandas dataframe weighting


【解决方案1】:

您可以将数据框乘以系列或映射索引:

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

【讨论】:

    猜你喜欢
    • 2018-07-09
    • 1970-01-01
    • 1970-01-01
    • 2018-01-24
    • 1970-01-01
    • 2011-10-22
    • 1970-01-01
    • 1970-01-01
    • 2019-07-19
    相关资源
    最近更新 更多