Concatenate distinct columns in two dataframes using pandas (and append similar columns)

后端 未结 4 1185
眼角桃花
眼角桃花 2020-12-17 05:29

My question is closely related to Pandas Merge - How to avoid duplicating columns but not identical.

I want to concatenate the columns that are diff

相关标签:
4条回答
  • 2020-12-17 05:49

    Using concat with groupby and first:

    pd.concat([df1, df2, df3], 1).groupby(level=0, axis=1).first()
    

       A  B  C  D  id name place  qty  unit
    0  a  b  c  d   1  Tom    NY    2    10
    1  a  b  c  d   2  Ron    TK    3    15
    2  a  b  c  d   3  Don   Lon    5    90
    3  a  b  c  d   4  Sam    Hk    4    49
    
    0 讨论(0)
  • 2020-12-17 05:55

    Using reduce from functools

    from functools import reduce
    reduce(lambda left,right: pd.merge(left,right), [df1,df2,df3])
    Out[725]: 
       id place name  qty  unit  A  B  C  D
    0   1    NY  Tom    2    10  a  b  c  d
    1   2    TK  Ron    3    15  a  b  c  d
    2   3   Lon  Don    5    90  a  b  c  d
    3   4    Hk  Sam    4    49  a  b  c  d
    
    0 讨论(0)
  • 2020-12-17 05:57

    You can extract only those columns from df2 (and df3 similarly) which are not already present in df1. Then just use pd.concat to concatenate the data frames:

    cols = [c for c in df2.columns if c not in df1.columns]
    df = pd.concat([df1, df2[cols]], axis=1)
    
    0 讨论(0)
  • 2020-12-17 06:06

    You can use nested merge

    merge_on = ['id','place','name','qty','unit']
    df1.merge(df2, on = merge_on).merge(df3, on = merge_on)
    
    
    
        id  place   name    qty unit    A   B   C   D
    0   1   NY      Tom     2   10      a   b   c   d
    1   2   TK      Ron     3   15      a   b   c   d
    2   3   Lon     Don     5   90      a   b   c   d
    3   4   Hk      Sam     4   49      a   b   c   d
    
    0 讨论(0)
提交回复
热议问题