【问题标题】:Split and column and merge with another column while concatenating in pandas在熊猫中连接时拆分和列并与另一列合并
【发布时间】:2021-01-14 07:53:54
【问题描述】:

根据另一列的值在 pandas 中创建值的串联

我有 2 个数据框

df1

NAME    CODE
andy    a,d
roger   b
danny   d
cole    

df2

CODE    MATERIAL
a       paper
b       plastic
b       metal
d       wood
e       glass

我想用材料列更新 df2 并在必要时连接

预期结果:

CODE    MATERIAL    NAME
a       paper       andy
b       plastic     roger
b       metal       roger
d       wood        andy, danny
e       glass   


我该怎么办?

【问题讨论】:

    标签: pandas concatenation


    【解决方案1】:

    可以先根据df1创建代码命名映射,然后使用映射在df2中创建需要的列:

    # revert NAME to CODE mapping as CODE to NAME
    code_2_name = df1.assign(CODE = lambda x: x.CODE.str.split(','))
                     .explode('CODE')
                     .groupby('CODE')
                     .agg(','.join)  
    code_2_name
    #            NAME
    #CODE            
    #a           andy
    #b          roger
    #d     andy,danny
    
    # create the NAME column based on the code to name mapping
    df2['NAME'] = df2.CODE.map(code_2_name.NAME)
    df2
    #  CODE MATERIAL        NAME
    #0    a    paper        andy
    #1    b  plastic       roger
    #2    b    metal       roger
    #3    d     wood  andy,danny
    #4    e    glass         NaN
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-08-01
      • 2023-02-04
      • 2022-07-14
      • 1970-01-01
      • 2018-12-24
      • 2017-05-28
      • 1970-01-01
      相关资源
      最近更新 更多