I'm trying to highlight specific number with different color in my dataframe below:
import pandas as pd
df = pd.DataFrame([[10,3,1], [3,7,2], [2,4,4]], columns=list("ABC"))
I can highlight a specific number by one color, for example:
def HIGHLIGHT_COLOR(x):
criteria = x == 4
return ['background-color: green' if i else '' for i in criteria]
df.style.apply(HIGHLIGHT_COLOR)
What I need is to highlight every individual number, here's my code but it doesn't work:
def HIGHLIGHT_COLOR(x):
if x == 4:
color = green
elif x == 2:
color = yellow
elif x == 3:
color = grey
elif x == 7:
color = purple
elif x == 10:
color = black
return f'color: {color}'
df.style.apply(HIGHLIGHT_COLOR)
Can anyone assist this? Thank you!
