【发布时间】:2021-12-25 21:57:20
【问题描述】:
我有两个数据框,想将 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”合并为映射的决定因素? - 它们在两个数据集中应该相等。
非常感谢任何帮助!
【问题讨论】:
标签: python pandas numpy mapping