基于两个数据框中的多列将值从一个 df 映射到另一个 df

Mapping values from one to the other df based on multiple columns in both dataframes

我有两个数据框,想将 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")

检查 here and here 以了解 merge 的工作原理。


结果

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