Click here to Skip to main content
15,936,460 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am comparing the source and target dataset (9000 rows, 14 columns) and highlighting the difference using below code. here using apply method I am calling the function. but the problem is, highlighting difference works till some rows, after that rows are not getting highlight differences. Not sure what is the limitation or am I missing something here?

Note: I am not sure how to attach the dataset file with this question but I am using sample csv with 9000 rows,14 columns from https://extendsclass.com/csv-generator.html

example here in first record, its highlighting the difference,but after ID 4144 its not getting highlighted

What I have tried:

Python
import pandas as pd
import numpy as np
from IPython.display import display, HTML

src = pd.read_csv('mysrc.csv')
tar = pd.read_csv('mytar.csv')
def highlight_diff(data, color='yellow'):
    attr = 'background-color: {}'.format(color)
    other = data.xs('Source', axis='columns', level=-1)
    return pd.DataFrame(np.where(data.ne(other, level=0), attr, ''),
                        index=data.index, columns=data.columns)
df_all = pd.concat([src.set_index('id'), tar.set_index('id')], 
                   axis='columns', keys=['Source', 'Target'])
df_final = df_all.swaplevel(axis='columns')[src.columns[1:]]
caption_styles = {
    'selector':'caption',
    'props':[('font-weight', 'bold'),('margin-bottom','25px'),('font-size','25px')]
}
select_styles = {  # for row hover use <tr> instead of <td>
    'selector': '',
    'props': [('border-collapse', 'collapse'),('margin', '100px auto')]
}

table_styles = {  # for row hover use <tr> instead of <td>
    'selector': 'table',
    'props': [('border-collapse', 'collapse'),('margin', '100px auto')]
}

tbody_styles = {  # for row hover use <tr> instead of <td>
    'selector': 'tbody',
    'props': [('border', '1px solid #A2DBFA')]
}
td_styles = {  # for row hover use <tr> instead of <td>
    'selector': 'td',
    'props': [('border', '1px solid #A2DBFA'),('padding','1em'),('border-bottom','2px solid #A2DBFA')]
}
th_styles = {  # for row hover use <tr> instead of <td>
    'selector': 'th',
    'props': [('border', '1px solid #A2DBFA'),('padding','1em'),('background-color','#39A2DB'),('border-bottom','2px solid #A2DBFA')]
}
df_out = df_final.style.set_table_styles([caption_styles,select_styles,table_styles, tbody_styles,td_styles, th_styles]).set_caption("Test Case 1").apply(highlight_diff, axis=None)
df_out
Posted
Updated 28-Jul-21 21:47pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900