Python Forum
Python pandas merge with or conditional
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python pandas merge with or conditional
#1
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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Assigning conditional values in Pandas Scott 3 700 Dec-19-2023, 03:10 AM
Last Post: Larz60+
  xlswriter(pandas) and conditional format paul18fr 1 1,130 Aug-28-2022, 07:56 AM
Last Post: paul18fr
  Python - Merge existing cells of Excel file created with xlsxwriter manonB 0 3,668 Mar-10-2021, 02:17 PM
Last Post: manonB
  Pandas: summing columns conditional on the column labels ddd2332 0 2,063 Sep-10-2020, 05:58 PM
Last Post: ddd2332
  Pandas merge question smw10c 2 5,648 Jul-02-2020, 06:56 PM
Last Post: hussainmujtaba
  Pandas dataframe merge snmmat 1 2,083 Mar-09-2020, 06:56 PM
Last Post: jefsummers
  Pandas merge csv files karlito 2 3,120 Dec-16-2019, 10:59 AM
Last Post: karlito
  Groupby in pandas with conditional - add and subtract rregorr 2 6,905 Jul-12-2019, 05:17 PM
Last Post: rregorr
  Pandas Dataframe conditional actions tom1983 1 2,768 Dec-06-2018, 11:47 PM
Last Post: MAZambelli4353
  Why can't I merge pandas dataframes learnpython2018 2 7,598 Sep-23-2018, 05:53 PM
Last Post: learnpython2018

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020