vaneesh dass
vaneesh dass

Reputation: 23

How to highlight dataframe based on another dataframe value so that the highlighted dataframe can be exported to excel

I have two dataframes of lets say same shape, need to compare each cell of dataframes with one another. If they are mismatched or one value is null then have to write the bigger dataframe to excel with highlighting cells where mismatched or null value was true.

i calculated the two dataframe differences as another dataframe with boolean values.

data1 = [['tom', 10], ['nick', 15], ['juli', 14]] 
data2=[['tom', 10], ['sam', 15], ['juli', 14]] 

# Create the pandas DataFrame 
df1 = pd.DataFrame(data, columns = ['Name', 'Age']) 
df2 = pd.DataFrame(data2, columns = ['Name', 'Age']) 
df1.replace(r'^\s*$', np.nan, regex=True, inplace=True)
df2= pd.read_excel(excel_file, sheet_name='res', header=None)
df2.replace(r'^\s*$', np.nan, regex=True, inplace=True)
df2.fillna(0, inplace=True)
df1.fillna(0, inplace=True)

difference = df1== df2 #this have boolean values True if value match false if mismatch or null

now i want to write df1 with cells highlighted according to difference. e.g if difference cell1 value is false the i want to higlight df1 cell1 as yellow and then write the whole df1 with highlights to excel.

here is df1 and df2 i want this as final answer. In final answer nick is highlighted(i want to highlight with background color).

i already tried using pandas Styler.applymap and Styler.apply but no success as two dataframe are involved. Maybe i am not able to think this problem straight.

df1:

enter image description here

df2:

enter image description here

enter image description here

Upvotes: 2

Views: 1244

Answers (1)

anky
anky

Reputation: 75080

you can do something like:

def myfunc(x):
    c1=''
    c2='background-color: red'
    condition=x.eq(df2)
    res=pd.DataFrame(np.where(condition,c1,c2),index=x.index,columns=x.columns)
    return res
df1.style.apply(myfunc,axis=None)

enter image description here

Upvotes: 2

Related Questions