Skip to content Skip to sidebar Skip to footer

Concatenate Distinct Columns In Two Dataframes Using Pandas (and Append Similar Columns)

My question is closely related to Pandas Merge - How to avoid duplicating columns but not identical. I want to concatenate the columns that are different in three dataframes. The d

Solution 1:

Using reduce from functools

fromfunctoolsimportreducereduce(lambda left,right: pd.merge(left,right), [df1,df2,df3])
Out[725]: 
   idplacenameqtyunitABCD01NYTom210abcd12TKRon315abcd23LonDon590abcd34HkSam449abcd

Solution 2:

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
01   NY      Tom     210      a   b   c   d
12   TK      Ron     315      a   b   c   d
23   Lon     Don     590      a   b   c   d
34   Hk      Sam     449      a   b   c   d

Solution 3:

Using concat with groupby and first:

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

AB  C  D  id name place  qty  unit
0ab  c  d   1  Tom    NY    2101ab  c  d   2  Ron    TK    3152ab  c  d   3  Don   Lon    5903ab  c  d   4  Sam    Hk    449

Solution 4:

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)

Post a Comment for "Concatenate Distinct Columns In Two Dataframes Using Pandas (and Append Similar Columns)"