Given these two dataframes, how do I get the intended output dataframe?
The long way would be to loop through the rows of the dataframe with iloc
and then use the map
function after converting df2
to a dict
to map the x and y to their score.
This seems tedious and would take long to run on a large dataframe. I'm hoping there's a cleaner solution.
df1:
ID A B C
1 x x y
2 y x y
3 x y y
df2:
ID score_x score_y
1 20 30
2 15 17
3 18 22
output:
ID A B C
1 20 20 30
2 17 15 17
3 18 22 22
Note: the dataframes would have many columns and there would be more than just x and y as categories (possibly in the region of 20 categories).
Thanks!