【问题标题】:Mapping values from one to the other df based on multiple columns in both dataframes基于两个数据帧中的多列将值从一个映射到另一个df
【发布时间】: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


    【解决方案1】:

    试试

    df1_new = df1.merge(df2, on=["PC1", "PC2", "PC3"], how="left")
    

    查看herehere 了解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
    

    【讨论】:

    • 其实不行。它返回空值。它应该映射列中的值相同的值。
    • @awi1100 嗯,在这里可以使用您提供的示例数据框吗?数据是否被剥离(一个 df 的末尾有空白,但另一个没有,等等)?
    • df1 有 7000 行长,而 df2 只有 243 行。也许与索引有关?应该有一些匹配,所以我不知道为什么我只为添加的 K1 列返回 nans
    • @awi1100 长度无关紧要。 (我已经添加了我的输入和我的结果。)我怀疑数据可能看起来很相似,但实际上并非如此,因为末尾有空格等。
    • 非常感谢你!这有帮助!
    猜你喜欢
    • 1970-01-01
    • 2023-03-21
    • 2019-10-17
    • 1970-01-01
    • 2017-04-20
    • 2023-03-15
    • 2022-01-24
    • 2020-09-18
    • 1970-01-01
    相关资源
    最近更新 更多