4

I have two Pandas dataframes that I would like to join on two columns. The columns are named differently in each df.

In [11]: df1
Out[11]: 
ID_1  ID_2  Hour
132   235   1
133   236   2
134   237   3

In [12]: df2
Out[12]: 
ID    Hour Price
132   1    17.2
133   2    14.6
134   3    21.3

I would like to get

In [13]: df3
Out[13]: 
ID_1  ID_2  Hour  ID    Price 
132   235   1     132   17.2
133   236   2     133   14.6
134   237   3     134   21.3

In SQL I would do something like the following:

select *
from df1
join df2 on df1.ID_1 = df2.ID
and df1.Hour = df2.Hour

I know the way to join on differently-named columns is, but this doesn't seem to allow for a second join on condition.

pd.merge(df1, df2, left_on='ID_1', right_on='ID', how='left')

...and the syntax to join on multiple identically-named columns is, but they aren't identically named...

pd.merge(df1, df2, how='left', on=['ID', 'Hour'])

1 Answers1

2

Try:

df1.merge(df2, left_on=['ID_1', 'Hour'], right_on=['ID', 'Hour'])

   ID_1  ID_2  Hour   ID  Price
0   132   235     1  132   17.2
1   133   236     2  133   14.6
2   134   237     3  134   21.3
piRSquared
  • 285,575
  • 57
  • 475
  • 624