I have a table similar to below:
Sector | 4/1/2022 | 5/1/2022 | 6/1/2022 |
---|---|---|---|
A | 0 | 05 | 12 |
B | 18 | 20 | 09 |
C | 02 | 09 | 12 |
I tried using the below code however it highlights the cell instead of changing font colour.
df.style.highlight_min(color = 'red', axis = 1)
Try this:
(df.iloc[:,1:].style.highlight_max(axis=1, props='color:green;')
.highlight_min(axis=1, props='color:red;'))
# or
(df.set_index('Sector').style.highlight_max(axis=1, props='color:green;')
.highlight_min(axis=1, props='color:red;'))
UPDATE
Based on this post with few changes the function works for your task without a need of the props
argument
def highlight_min_max(x):
color_min = 'color: green'
color_max = 'color: red'
# condition
mask_min = x.eq(x[x>.01].min()) #excluding 0 when searching for min
mask_max = x.eq(x.max())
# empty Series of styles
s = pd.Series('', index=x.index)
# set columns by conditions
return s.mask(mask_min, color_min).mask(mask_max, color_max)
df.set_index('Sector').style.apply(highlight_min_max, axis=1)