I have two pandas dataframes. They have the same winner and loser name pairs but their are in different orders in the two dataframes.
DF1
Winner Loser RankW RankL
0 Fleishman Z. Calleri A. 170.0 26.0
1 Roddick A. Tsonga J.W. 7.0 212.0
2 Gasquet R. Volandri F. 17.0 45.0
DF2
Winner Loser WHand LHand
0 Gasquet R. Volandri F. R R
1 Fleishman Z. Calleri A. L R
2 Roddick A. Tsonga J.W. R R
I want to merge them in one single Dataframe, however, whenever I try it I get additional rows. What I want to get is:
Winner Loser RankW RankL WHand LHand
0 Fleishman Z. Calleri A. 170.0 26.0 L R
1 Roddick A. Tsonga J.W. 7.0 212.0 R R
2 Gasquet R. Volandri F. 17.0 45.0 R R
Thus, I want to merge them following the order of the pairs in DF1 but adding the corrisponing values of WHand and LHand in DF2.
I know that all pairs correspond because I tried to determine the rows in DF1 which were not in DF2 but there are none.
names = DF2[['Winner','Loser']]
df = DF1.merge(names, on=['Winner','Loser'],how = 'outer' ,indicator=True).loc[lambda x : x['_merge']=='left_only']
len(df)
Out: 0