I have two Dataframes of the same length which have both a 'winner' and 'loser' columns. The order of the rows in the two dataframes differ but the pair 'winner','loser' is the same in both dataframes. The problem that I am facing is that there are certain names in winner and loser that are almost similar but have some differences but i know that they correspond to the same persons. A following:
DF1
winner loser winnerpts loserpts
0 Rachel Thomas 5 1
1 Jason Tom 8 6
2 JenniferS. Tony 10 5
......
DF2
winner loser Agew Agel
0 Jennifer Tony 25 23
1 Rachel Thomas 27 30
2 Jason Tom R. 22 24
......
What I want to do is to transform the second Dataframe so as it has the same row order of the first one and the same names changed. As following:
winner loser Agew Agel
0 Rachel Thomas 27 30
1 Jason Tom 22 24
2 JenniferS. Tony 25 23
......
What I was thinking of doing was to crate a dictionary with the names of winner in the first Dataframe and the indexes in which that name is found in the Dataframe and the same for the second Dataframe. Then, change the indexes of the second Dataframe with those of the first and use the modified dictionary to reorder the Dataframe as following:
WG1 = DF1['Winner'].tolist()
WG2 = DF2['Winner'].tolist()
DF1= dict()
for i, item in enumerate(WG1):
if item not in DF1:
DF1[item] = [i]
else:
DF1[item].append(i)
DF2= dict()
for i, item in enumerate(WG2):
if item not in DF2:
DF2[item] = [i]
else:
DF2[item].append(i)
modified_D2 = {k:(DF2[k] if k in DF2 else v) for k,v in DF1.items()}
With v the indexes of the rows and k the name of the winner of course according to the first Dataframe. However, I do not know how to apply it to the second Dataframe.Is there a way to do it?
Can you please help me to change my code? Thank you in advance