I have a pandas dataframe with a few columns. I am trying to color the cells in one of the columns and I have been partially successful so far.
In the column color_me
I want to:
- For all the rows but the last one, I will be coloring according to a specific scale
- In the last row of that column (therefore, just one cell), I want to color based on another scale. It is the 9th row of that column.
I was able to do 1., but I am failing to do 2. I don't know how to highlight just one specific cell if I have to put a conditional on which color it should be having.
df = pd.DataFrame({'metric': ['metric0', 'metric1', 'metric2', 'metric3', 'metric4', 'metric5', 'metric6', 'metric7', 'metric8', 'metric9'],
'column1': [80, 12, 110, 100.2, 39, 29, 48, 78, 93, 100.8],
'color_me': [89, 102, 43, 112, 70, 38, 80, 110, 93, 100.3]})
def highlight_SLA(series):
green = 'background-color: lightgreen'
yellow = 'background-color: yellow'
pink = 'background-color: pink'
return [green if value <= 90 else yellow if value <100 else pink for value in series]
def highlight_specific_cell(series):
green = 'background-color: lightgreen'
yellow = 'background-color: yellow'
pink = 'background-color: pink'
return [green if value >= 100.2 else yellow if value >100 else pink for value in series]
slice_SLA = ['color_me']
slice_SLA_index = ['color_me'][9]
(df.style
.apply(highlight_SLA, subset = slice_SLA)
.apply(highlight_SLA_availability_index, subset = slice_SLA_index)
)
The second apply
doesn't work as I wanted. It is supposed to give a green background to the that has a value of 100.3
.
Should I select its subset in another way? It seems that slice_SLA_index = ['color_me'][9]
is not the way to do it, but I don't know how to fix this.