Python Forum

Full Version: Python pandas merge with or conditional
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am merging two dataframes that have 3 common fields. The issue is that if you cannot find the value you are looking for you should look for another one that matches 2 of the fields or only 1 and I don't know how to do it in pandas.

In the perfect situation we will find the 3 common fields in the 2 data frames:

'OFICINA_A' 'INC_A' 'N____A' -> 'OFICINA_B' 'INC_B' 'N____B'

if it does not find it will try with 2:

'INC_A' 'N____A' -> 'INC_B' 'N____B'

or:

'OFICINA_A' 'N____A' -> 'OFICINA_B' 'N____B'

I have tried this code but show error:

df_query = pd.merge(df_dip, df_2, how='left', (left_on=['Oficina_a', 'Incidencia_a', 'Codigo_a'], right_on=['Oficina_b', 'Incidencia_b', 'Codigo_b']) | (left_on=['Incidencia_a', 'Codigo_a'], right_on=['Incidencia_b', 'Codigo_b'])) 
Can you help me?

Thanks!