python - 基于两个数据帧中的多列将值从一个映射到另一个df
问题描述
我有两个数据框,并希望将 df2 中的“K1”值映射到 df1 中的新列 [“K1_mapped”],但仅当列值 [[“PC1”、“PC2”和“PC3”]] 是在两个数据框中相同。
DF1:
PC1 PC2 PC3
1 Kunststoffe Dachbahnen Elastomer-Dachbahnen
2 Kunststoffe Profile Plastikprofile
3 Kunststoffe Dachbahnen Elastomer-Dachbahnen
4 Kunststoffe Dachbahnen Elastomer-Dachbahnen
5 Kunststoffe Dachbahnen Elastomer-Dachbahnen
6 Kunststoffe Dachbahnen Elastomer-Dachbahnen
7 Holz Vollholz Brettschichtholzplatte
8 Holz Holz Holz
9 Holz Vollholz Brettschichtholzplatte
10 Baustoffe Steine Dachziegel
11 Daemmstoffe Holzfasern Holzfaserdaemmplatte
12 Daemmstoffe Holzfasern Holzfaserdaemmplatte
DF2:
PC1 PC2 PC3 K1
1 Holz Holz Holz Boeden und Decken
2 Holz Vollholz Balkenschichtholz Boeden und Decken
3 Holz Vollholz Bau-Schnittholz Waende
4 Holz Vollholz Brettschichtholz (BSH) Waende
5 Holz Vollholz Brettschichtholzplatte Waende
6 Kunststoffe Dachbahnen Elastomer-Dachbahnen Dachkonstruktion
7 Kunststoffe Dachbahnen PVC-Dachbahnen Dachkonstruktion
8 Kunststoffe Dichtmassen Acrylat Waende
9 Kunststoffe Profile Plastikprofile Fenster
10 Daemmstoffe Holzfasern Holzfaserdaemmplatte Isolierung
所需输出 DF1_new:
PC1 PC2 PC3 K1
1 Kunststoffe Dachbahnen Elastomer-Dachbahnen Dachkonstruktion
2 Kunststoffe Profile Plastikprofile Fenster
3 Kunststoffe Dachbahnen Elastomer-Dachbahnen Dachkonstruktion
4 Kunststoffe Dachbahnen Elastomer-Dachbahnen Dachkonstruktion
5 Kunststoffe Dachbahnen Elastomer-Dachbahnen Dachkonstruktion
6 Kunststoffe Dachbahnen Elastomer-Dachbahnen Dachkonstruktion
7 Holz Vollholz Brettschichtholzplatte Waende
8 Holz Holz Holz Boeden und Decken
9 Holz Vollholz Brettschichtholzplatte Waende
10 Baustoffe Steine Dachziegel NAN
11 Daemmstoffe Holzfasern Holzfaserdaemmplatte Isolierung
12 Daemmstoffe Holzfasern Holzfaserdaemmplatte Isolierung
我试图映射 K1,但它只使用 PC1 进行映射:
d1['K1_mapped'] = d1['PC1'].map(d2.drop_duplicates('PC1').set_index('PC1')['K1'])
但是如何将“PC2”和“PC3”作为映射的决定因素?- 它们在两个数据集中应该相等。
任何帮助深表感谢!
解决方案
尝试
df1_new = df1.merge(df2, on=["PC1", "PC2", "PC3"], how="left")
结果为
df1 = pd.DataFrame(
{'PC1': ['Kunststoffe', 'Kunststoffe', 'Kunststoffe', 'Kunststoffe', 'Kunststoffe', 'Kunststoffe', 'Holz', 'Holz', 'Holz', 'Baustoffe', 'Daemmstoffe', 'Daemmstoffe'],
'PC2': ['Dachbahnen', 'Profile', 'Dachbahnen', 'Dachbahnen', 'Dachbahnen', 'Dachbahnen', 'Vollholz', 'Holz', 'Vollholz', 'Steine', 'Holzfasern', 'Holzfasern'],
'PC3': ['Elastomer-Dachbahnen', 'Plastikprofile', 'Elastomer-Dachbahnen', 'Elastomer-Dachbahnen', 'Elastomer-Dachbahnen', 'Elastomer-Dachbahnen', 'Brettschichtholzplatte', 'Holz', 'Brettschichtholzplatte', 'Dachziegel', 'Holzfaserdaemmplatte', 'Holzfaserdaemmplatte']}
)
df2 = pd.DataFrame(
{'PC1': ['Holz', 'Holz', 'Holz', 'Holz', 'Holz', 'Kunststoffe', 'Kunststoffe', 'Kunststoffe', 'Kunststoffe', 'Daemmstoffe'],
'PC2': ['Holz', 'Vollholz', 'Vollholz', 'Vollholz', 'Vollholz', 'Dachbahnen', 'Dachbahnen', 'Dichtmassen', 'Profile', 'Holzfasern'],
'PC3': ['Holz', 'Balkenschichtholz', 'Bau-Schnittholz', 'Brettschichtholz (BSH)', 'Brettschichtholzplatte', 'Elastomer-Dachbahnen', 'PVC-Dachbahnen', 'Acrylat', 'Plastikprofile', 'Holzfaserdaemmplatte'], 'K1': ['Boeden und Decken', 'Boeden und Decken', 'Waende', 'Waende', 'Waende', 'Dachkonstruktion', 'Dachkonstruktion', 'Waende', 'Fenster', 'Isolierung']}
)
是
PC1 PC2 PC3 K1
0 Kunststoffe Dachbahnen Elastomer-Dachbahnen Dachkonstruktion
1 Kunststoffe Profile Plastikprofile Fenster
2 Kunststoffe Dachbahnen Elastomer-Dachbahnen Dachkonstruktion
3 Kunststoffe Dachbahnen Elastomer-Dachbahnen Dachkonstruktion
4 Kunststoffe Dachbahnen Elastomer-Dachbahnen Dachkonstruktion
5 Kunststoffe Dachbahnen Elastomer-Dachbahnen Dachkonstruktion
6 Holz Vollholz Brettschichtholzplatte Waende
7 Holz Holz Holz Boeden und Decken
8 Holz Vollholz Brettschichtholzplatte Waende
9 Baustoffe Steine Dachziegel NaN
10 Daemmstoffe Holzfasern Holzfaserdaemmplatte Isolierung
11 Daemmstoffe Holzfasern Holzfaserdaemmplatte Isolierung
推荐阅读
- java - 如何在 Java 中处理来自 Jackson 解析器的 `jsonParseException`
- angular - 动态 FormGroup 上的 ngFor 未更新
- r - 在R中按日期过滤列
- javascript - 自动滚动反应 redux 实现
- javascript - 如何捕获没有键和值的json数据
- javascript - 我有这个错误“cURL 错误 60:SSL 证书问题:无法获取本地颁发者证书”
- python - Python SQL 通过多个查询循环变量
- web-scraping - 错误:谷歌表中 importhtml 函数中的“导入内容为空”
- python - 如何删除字符串列表中的特殊字符并将其拆分为单独的元素
- if-statement - 多个单元格引用的过滤公式