Home > Enterprise >  How to compare two columns values in two dataframes in Python?
How to compare two columns values in two dataframes in Python?

Time:12-05

I have two dataframes and I have created a "Check" column in second dataframe to check if values in the Total Claims columns are equal. Here are my two dataframes:

Dataframe 1

enter image description here

Dataframe 2

enter image description here

The code that I used to create the "Check" column comparing Total Claims between the two dataframes was:

reported_claims['Check'] =  np.where(reported_claims['Total Claims'].reset_index(drop=True) == df['Total Claims'].reset_index(drop=True) , 'TRUE', 'FALSE')

I noticed that the 7th values in two dataframes are both 31.32 but the check columns says False. I was just wondering why is it saying False and how would I fix this problem? Thank you so much for your time!

CodePudding user response:

The issue arises by comparing two floating point numbers. The general rule for most programming languages (including python) is that you can never check if two floats are exactly equal. You have to instead check if the two values are close. You can do so using the np.isclose(). Check this answer for a reference on how to use it.

CodePudding user response:

Use merge command to join two dataframe and add check column to compare two columns

df = pd.merge(left=Dataframe1, right=Dataframe2, left_on='month', right_on='month')
  • Related