0

I want to join two tables by merge, matching 3 common variables in both tables:

  • Df1
  job         marital month valor
    unemployed   married june   7
    entrepreneur married august 9
  • Df2
 job         marital month valor1
    unemployed   married june   10
    entrepreneur married august 15

I´m using that:

  df_nuevo=pd.merge(df1, df2, on='month' and 'marital' and 'job', how='left')

And I´d want to obtain:

  • New df:
   job         marital month valor valor1
    unemployed   married june   7    10
    entrepreneur married august 9    15

But the record are multiplied.

Thanks in advance

fcaste
  • 13
  • 2

1 Answers1

0

During merges if you don't set the "on" parameter, pandas will use all columns whose names matche between the two dataframes to merge. Something like the following should work:

df_nuevo = pd.merge(df1, df2, how='left')

However this can lead to unseen errors if the columns aren't exact matches (example: ' marital', vs 'marital') so it can be more consistent to specify all 3 columns in a list using the on=['month', 'marital', 'job'] setup that @chris commented.

lolzebra
  • 76
  • 3