How to Find Differences In Dataframe Rows

Python

In this code snippet we compare two rows in the same dataframe or different dataframes with the same columns. Where there are differences the column name and the column values from each row are printed for each differing column.

 1|  columns_to_compare = ['a', 'b', 'c']
 2|  
 3|  # Compare rows in the same dataframe:
 4|  for column in columns_to_compare:
 5|      if df[column][0] != df[column][1]:
 6|          print(column)
 7|          print(df[column][0])
 8|          print(df[column][1])  
 9|  
10|  # Compare rows in different dataframes:
11|  for column in columns_to_compare:
12|      if df1[column][0] != df2[column][0]:
13|          print(column)
14|          print(df1[column][0])
15|          print(df2[column][0])
Did you find this snippet useful?

Sign up for free to to add this to your code library