join two pandas dataframe using a specific column
40,608
Solution 1
pd.concat([df1.set_index('A'),df2.set_index('A')], axis=1, join='inner')
If you wish to maintain column A
as a non-index, then:
pd.concat([df1.set_index('A'),df2.set_index('A')], axis=1, join='inner').reset_index()
Solution 2
Alternatively, you could just do:
df3 = df1.merge(df2, on='A', how='inner', suffixes=('_1', '_2'))
And then you can keep track of each value's origin
Comments
-
ahajib almost 2 years
I am new with pandas and I am trying to join two dataframes based on the equality of one specific column. For example suppose that I have the followings:
df1 A B C 1 2 3 2 2 2 df2 A B C 5 6 7 2 8 9
Both dataframes have the same columns and the value of only one column (say A) might be equal. What I want as output is this:
df3 A B C B C 2 8 9 2 2
The values for column 'A' are unique in both dataframes.
Thanks