0

I have two dataframes as below: I want to compare two dataframes row by row and if suppose row1 of df1 is not equal to row1 of df2 print an error

df1

    A     B  
0   1     3
1   2     4 
2   3     5 
3   4     6 
4   5     7 

df2

    A     B  
0   1     3
1   2     4 
2   3     5 
3   4     5
4   5     7

I want to print an error for row#4 because df1 has the value of 6 for variable 'B' and df1 has the value of 5

Shanoo
  • 1,185
  • 1
  • 11
  • 38

3 Answers3

1

Did you take a look at the documentation?

df1.eq(df2)

      A      B
0  True   True
1  True   True
2  True   True
3  True  False
4  True   True

If you want to see the specific values and rows you can do this

df1[~df1.eq(df2)].dropna(how='all')

    A    B
3 NaN  6.0
gold_cy
  • 13,648
  • 3
  • 23
  • 45
  • I want to print an error for each row where that row has unequal value. So in that case we need to iterate through each row – Shanoo Feb 27 '19 at 21:16
  • no you don't iterate through a `pandas` dataframe row wise, that's not using it correctly – gold_cy Feb 27 '19 at 21:20
0

I like @aws_apprentice's answer. But, since you asked to "print an error", consider also pandas.testing.assert_frame_equal (docs), which will raise an AssertionError exception if the DataFrames are not identical and give you diagnostic output.

ralex
  • 378
  • 1
  • 6
0

you should check Andy Hayden's answer here: Outputting difference in two Pandas dataframes side by side - highlighting the difference

what you are trying to do(print error if a row is different) may not be the best option here. which dataframe do you intend to uses as a basis for comparison and add the error column? suppose you choose df1 and compare it to df2, what if df2 has additional rows that are not present in df1; in this case there is no row in df1 to add the error msg.

Hkk
  • 111
  • 1
  • 9