Home > Software engineering >  Comparing 2 columns in same dataframe and display unmatch records
Comparing 2 columns in same dataframe and display unmatch records

Time:09-30

I am trying to compare 2 columns values from same dataframe and display rows with unmatch values. However, there are str value type in column Profit.

diff = c.loc[~(c['Profit'] == c['Profit1'])]

CodePudding user response:

You can force data types when comparing the two columns to make sure the columns are the same.

diff = c.loc[~c['Profit'].astype(int,errors='ignore') == c['Profit1'].astype(int,errors='ignore')]

CodePudding user response:

This should work fine, I create a sample df with strings and ints

df = pd.DataFrame({'Profit':['A',2,'C','B','C','A','B','A',3],'Profit1':['B',2,'B','D','D','A','B','D',4]})

Now we can get the rows with unmatched values with

df[df["Profit"]!= df["Profit1"]]
  • Related