1

I have 2 Dataframes that I'd like to combine in Pandas where two 2 conditions are met, but I'm not successfully getting there. Thanks in advance for assistance!

df1
    A    B    C
0   1    2    3
1   4    5    6
2   7    8    9

df2
    A    B    F
0   1    2    cat
1   4    5    dog
2   7    8    moose

Desired Result

df3
    A    B    C    F
0   1    2    3    cat
1   4    5    6    dog
2   7    8    9    moose

Attempts to merge that are failing that are failing:

  • pd.merge(df1, df2, on='A' & 'B', how='left')
  • pd.merge(df1, df2, on('A' & 'B'), how='left')
  • pd.merge(df1, df2, on='A' & on='B',how='left')
Levine
  • 109
  • 2
  • 12

1 Answers1

1

Maybe you can try add to merge multiple join keys on=['A','B']:

print pd.merge(df1, df2, on=['A','B'], how='left')

   A  B  C      F
0  1  2  3    cat
1  4  5  6    dog
2  7  8  9  moose

Docs.

jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252