Pandas: join DataFrames on field with different names? Pandas: join DataFrames on field with different names? python python

Pandas: join DataFrames on field with different names?


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.


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')