Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python pandas dataframe join two dataframes [duplicate]

I am trying to join to data frames. They look like this

DF1 = ID     COUNTRY     YEAR     V1     V2     V3    V4
      12     USA         2012     x      y      z      a
      13     USA         2013     x      y      z      a
      14     RUSSIA      2012     x      y      z      a

DF2 = ID     COUNTRY     YEAR     TRACT
      9      USA         2000       A
      13     USA         2013       B

The desired end goal is:

DF3 = ID     COUNTRY     YEAR     V1     V2     V3    V4    TRACT    
      9      USA         2000                                 A
      12     USA         2012     x      y      z      a
      13     USA         2013     x      y      z      a      B
      14     RUSSIA      2012     x      y      z      a

I've been trying to use the pd.merge and the .join function with the on='outer' setting to no success

df3 = pd.merge(df1,df2,how='outer',left_on=['ID','Country','Year'],right_on=['ID',"Country","Year"])
like image 773
bjurstrs Avatar asked Feb 21 '15 04:02

bjurstrs


People also ask

How do I avoid duplicates when merging pandas?

In this approach to prevent duplicated columns from joining the two data frames, the user needs simply needs to use the pd. merge() function and pass its parameters as they join it using the inner join and the column names that are to be joined on from left and right data frames in python.

How do I get rid of duplicate columns while merging pandas?

To drop duplicate columns from pandas DataFrame use df. T. drop_duplicates(). T , this removes all columns that have the same data regardless of column names.


2 Answers

try this:

df.merge(df2,how='outer',left_on=['ID','COUNTRY','YEAR'],right_on=['ID',"COUNTRY","YEAR"])

(the column names should be in caps based on your input tables)

like image 186
JAB Avatar answered Oct 05 '22 13:10

JAB


Have you tried

df1.join(df2)

You can add parameters later, but it should work.

like image 40
Harvey Avatar answered Oct 05 '22 14:10

Harvey