Python Forum
dataframe cell conditional format by row - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: dataframe cell conditional format by row (/thread-33387.html)



dataframe cell conditional format by row - SriRajesh - Apr-24-2021

Hi,
I have below dataframe:
df:
Name gender age  rank code
Mek  m      29   2     23
ABK  f      20   23    56
SGH  f      23   19    12
JKL  m      17   33    19
threshold_mek = 12
threshold_ABK = 20
threshold_SGH = 19
threshold_JKL = 13

I want to make font color red if any value in particular row not_equal to threshold

I use below code, but when I open the excel it did not highlight anything


import pandas as pd
df=pd.read_csv('input.csv')
df.style.apply(lambda x:['background:red'' if x <threshold_mek  else 'background:green' for x in df.loc[0]], axis=0)
df.style.apply(lambda x:['background:red'' if x <threshold_ABK else 'background:red' for x in df.loc[1]], axis=0)
df.style.apply(lambda x:['background:red'' if x <threshold_SGH else 'background:red' for x in df.loc[1]], axis=0)
df.style.apply(lambda x:['background:red'' if x <threshold_JKL else 'background:red' for x in df.loc[1]], axis=0)
df.styled.to_excel('styled.xlsx',engine='openpyxl',index=False)
I want change the font color red if the any cell value > the row threshold from 2nd column to end in each row. Ican see the color in Jupiter but when write to excel and open excel not visible any color.