Python Forum
dataframe cell conditional format by row
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
dataframe cell conditional format by row
#1
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Smile Set 'Time' format cell when writing data to excel and not 'custom' limors 3 752 Mar-29-2021, 09:36 PM
Last Post: Larz60+
  display the result of Dataframe in tabulate format alex80 0 428 Sep-09-2020, 02:22 PM
Last Post: alex80
  openpyxl, if value in cell then change format genderbee 1 2,446 Nov-05-2019, 01:37 PM
Last Post: genderbee

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020