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
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
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
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
Solution 3:
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
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)"