python - 多个基于不同的数据框
问题描述
我有两个数据框:df1:
Name Segment Axis 1 2 3 4 5
Amazon 1 slope NaN 2.5 2.5 2.5 2.5
Amazon 1 x 0.0 1.0 2.0 3.0 4.0
Amazon 1 y 0.0 0.4 0.8 1.2 1.6
Amazon 2 slope NaN 2.0 2.0 2.0 2.0
Amazon 2 x 0.0 2.0 4.0 6.0 8.0
Amazon 2 y 0.0 1.0 2.0 3.0 4.0
df2:
Name Segment Cost
Amazon 1 100
Amazon 2 112
Netflix 1 110
Netflix 2 210
我想将第 1-5 列中“斜率”上的所有值乘以第二个数据框中的相应成本。
预期输出:
Name Segment Axis 1 2 3 4 5
Amazon 1 slope NaN 250 250 250 250
Amazon 1 x 0.0 1.0 2.0 3.0 4.0
Amazon 1 y 0.0 0.4 0.8 1.2 1.6
Amazon 2 slope NaN 224 224 224 224
Amazon 2 x 0.0 2.0 4.0 6.0 8.0
Amazon 2 y 0.0 1.0 2.0 3.0 4.0
解决方案
尝试这个:
#merge df2 to align to df1
u = df1.merge(df2,on=['Name','Segment'],how='left')
#find columns to multiply the cost
cols = df1.columns ^ ['Name','Segment','Axis']
#multiply and assign back
df1[cols] = u[cols].mul(u['Cost'],axis=0).where(df1['Axis'].eq('slope'),df1[cols])
print(df1)
Name Segment Axis 1 2 3 4 5
0 Amazon 1 slope NaN 250.0 250.0 250.0 250.0
1 Amazon 1 x 0.0 1.0 2.0 3.0 4.0
2 Amazon 1 y 0.0 0.4 0.8 1.2 1.6
3 Amazon 2 slope NaN 224.0 224.0 224.0 224.0
4 Amazon 2 x 0.0 2.0 4.0 6.0 8.0
5 Amazon 2 y 0.0 1.0 2.0 3.0 4.0
推荐阅读
- javascript - 确定是否在代理处理程序中访问或调用函数
- javascript - 如何将 Wiris 或 Mathjax 添加到 Quill 编辑器
- javascript - 构造函数没有保存 querySelector(x).value
- go - 如何在数据库更改时重新评估 promhttp.Handler?
- python - 使用具有许多 url 的 excel 文件生成站点地图(excel 到站点地图)
- php - 如何根据购物车中物品的运输类别禁用付款方式?
- python - 使用带有 numpy 数组的 PuLP 求解 LP
- html - 如何在具有 [position:relative] 的父元素内更改具有 [position:absolute] 的子元素的大小?
- javascript - Lookbehind 替代方案,同时具有lookbehind 和lookahead
- intellij-idea - WebStorm:按模式排除文件夹