Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas: join DataFrames on field with different names?

According to this documentation I can only make a join between fields having the same name.

Do you know if it's possible to join two DataFrames on a field having different names?

The equivalent in SQL would be:

SELECT * FROM df1 LEFT OUTER JOIN df2   ON df1.id_key = df2.fk_key 
like image 278
woshitom Avatar asked Sep 17 '14 10:09

woshitom


People also ask

Can you merge two DataFrames of different lengths pandas?

It can be done using the merge() method. Below are some examples that depict how to merge data frames of different lengths using the above method: Example 1: Below is a program to merge two student data frames of different lengths.


1 Answers

I think what you want is possible using merge. Pass in the keyword arguments for left_on and right_on to tell Pandas which column(s) from each DataFrame to use as keys:

pandas.merge(df1, df2, how='left', left_on=['id_key'], right_on=['fk_key']) 

The documentation describes this in more detail on this page.

like image 189
Alex Riley Avatar answered Sep 20 '22 04:09

Alex Riley