5

Elaborating from this question, I am trying to match two Pandas dataframes.

The matching condition is:

(left_df.column_to_match == right_df.first_column) | (left_df.column_to_match == right_df.second_column )

or, in words, the column to match in the left dataframe should be equal to either first or second column in the right dataframe - thus the OR condition.

I can make a workaround using pd.merge and inputting lists such as

left_df.merge(right_df, left_on=['to_match', 'to_match'], right_on=['first_column', 'second_column'])

but this, in turn, gives me only the AND condition result when both columns match. That is, the two columns in right_df have the same value.

This is an example of input data

// left df                           // right df
    To Match                             First       Second   
0   TCNU4843483                      0   ASDREF      TCNU4843483    
1   MA18219                          1   MA18219     Null
2   MA81192                          2   Null        Null
3   MFREIGHT                         3   HROB789     NESU6748392

and this of the expected output

    To Match          First       Second   
0   TCNU4843483       ASDREF      TCNU4843483   
1   MA18219           MA18219     Null
2   MA81192           Null        Null
3   MFREIGHT          Null        Null
4   Null              HROB789     NESU6748392

Any idea about whether Pandas support this, or I have to write my own function?

Mattia Paterna
  • 1,268
  • 3
  • 15
  • 31

0 Answers0