Join two pandas using a specific column

I am new to pandas and I am trying to combine two data frames based on the equality of one particular column. For example, suppose I have the following:

df1 ABC 1 2 3 2 2 2 df2 ABC 5 6 7 2 8 9 

Both data files have the same columns, and the value of only one column (for example, A) can be the same. What I want as output:

 df3 ABCBC 2 8 9 2 2 

The values ​​for column "A" are unique in both data frames.

thanks

+6
source share
2 answers
 pd.concat([df1.set_index('A'),df2.set_index('A')], axis=1, join='inner') 

If you want to save column A as a non-index, then:

 pd.concat([df1.set_index('A'),df2.set_index('A')], axis=1, join='inner').reset_index() 
+5
source

Alternatively, you can simply do:

 df3 = df1.merge(df2, on='A', how='inner', suffixes=('_1', '_2')) 

And then you can track every source start

+7
source

All Articles