Comparing two dataframes and highlighting the differences

34 Views Asked by At

I have two data frames with the same column names in the same order but difference lengths. I'm trying to compare the two dataframes and highlight the differences. Everytime I try, I get an error come up 'Can only compare identically-labeled DataFrame objects' if I manage to get it to work by ignoring the index using the equal method the second part of the code fails ('function' object has no attribute 'style')

The dataframes are a mixture of address, reference numbers and names

I want my code to look something like this-

df_mask = df.compare(df2, keep_shape = True).notnull().astype ('int')
df_compare = df.compare(df2, keep_shape= True, keep_equal = True, ignore_index=True)

def apply_colour(x):
    colors = {1:'lightblue', 0:'white'}
    return df_mask.applymap(lambda val: 'background-color: {}'.format(colors.get(val,'')))
df.compare.style.apply(apply_color, axis=None)

This is the code I tried which resulted in a 'Can only compare identically-labeled DataFrame objects'

0

There are 0 best solutions below