Python Forum
Python pandas merge with or conditional - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: Python pandas merge with or conditional (/thread-26626.html)



Python pandas merge with or conditional - Lafayette - May-07-2020

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!