Skip to content Skip to sidebar Skip to footer

Styling Pandas Based On Conditions

I am trying to color the text or cells of data frame based on the condition. This is the code I have. It works: def Highlight_Majors(val): color = 'blue' if val == 'Austria' e

Solution 1:

Use the in operator with a set membership test:

def Highlight_Majors(val):
    return'color: %s' % ('blue'ifvalin {"Austria", "Belgium"} else'black')

Solution 2:

How about this?

color = 'blue' if any([val==i for i in ["Austria", "Belgium"]]) else 'black'

Post a Comment for "Styling Pandas Based On Conditions"