1 Upvote

How to Find Differences In Dataframe Rows

Python
Visualisation & EDA

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.

columns_to_compare = ['a', 'b', 'c']

# Compare rows in the same dataframe:
for column in columns_to_compare:
    if df[column][0] != df[column][1]:
        print(column)
        print(df[column][0])
        print(df[column][1])  

# Compare rows in different dataframes:
for column in columns_to_compare:
    if df1[column][0] != df2[column][0]:
        print(column)
        print(df1[column][0])
        print(df2[column][0])

By detro - Last Updated June 4, 2022, 10:15 p.m.

Did you find this snippet useful?

Sign up to bookmark this in your snippet library

COMMENTS
RELATED SNIPPETS
2
Top Contributors
103
100