I would like to merge two dataframes df1 and df2, with the condition that if a value of the right-side in key2 is not present on the left-side in key1, than the alternative_key is used. Is there any way to do that in a nice way?
a = {'key1': ['a','b','c'], 'alternative_key':['f','g','h']}
df1 = pd.DataFrame(data=a)
b = {'key2':['a','b','h'], 'some_stuff': [1,2,3]}
df2 = pd.DataFrame(data=b)
df_final=df1.merge(df2, left_on='key1', right_on='key2', how='left')
expected result in df_final:
key1 alternative_key some_stuff
0 a f 1
1 b g 2
2 c h 3