Skip to content
Advertisement

Comparing Two dataframes of pandas on the basis of condition of two columns

I have two dataframe in which columns are different, And I need to search and the map the data in new file. I am sharing the dataframes and also desired output

DF1

JavaScript

DF2

JavaScript

Now I need to a output from above two dataframes.

Conditions

  1. Need to search Ref.Y in STR2, if available then pick the “Type” for output.
  2. if Ref.Y is “NA” then need to search Ref.X in STR1, if available then pick the “Type” for output.
  3. Also I need “Ref.No” in desired output correspond to the “Type” Ref.X or Ref.Y found in DF2

Desired Output

JavaScript

Thanks.

Advertisement

Answer

You can try repace NA string to <NA> type then use np.where

JavaScript

Or you can directly compare with NA string:

JavaScript
JavaScript

To get Ref.No correspond to the “Type” Ref.X or Ref.Y found in DF2, you can drop the NAN column in Int_Type

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement