user308827
user308827

Reputation: 21961

Perform concat in pandas without including common column twice

From Pandas documentation:

result = pd.concat([df1, df4], axis=1, join='inner')

enter image description here

How can I perform a concat operation but without including the common columns twice? I only want to include them once. In this example, columns B and D are repeated twice after concat but have the same values.

Upvotes: 1

Views: 564

Answers (1)

DYZ
DYZ

Reputation: 57033

Select the columns from df4 that are not in df1:

result = pd.concat([df1, df4[['F']]], axis=1, join='inner')

Or:

complementary = [c for c in df4 if c not in df1]
result = pd.concat([df1, df4[complementary], axis=1, join='inner')

The latter expression will choose the complementary columns automatically.

P.S. If the columns with the same name are different in df1 and df4 (as it seems to be in your case), you can apply the same trick symmetrically and select only the complementary columns from df1.

Upvotes: 3

Related Questions