Pandas: join DataFrames on field with different names?

PythonPandasJoinDataframeField

Python Problem Overview


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

Python Solutions


Solution 1 - Python

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.

Solution 2 - Python

df2['id_key'] = df2['fk_key'].str.lower()

df1['id_key'] = df1['id_key'].str.lower()

Now try to merge the dataframes

df3 = pd.merge(df2,df1,how='inner', on='id_key')

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionwoshitomView Question on Stackoverflow
Solution 1 - PythonAlex RileyView Answer on Stackoverflow
Solution 2 - PythonVipul SaxenaView Answer on Stackoverflow