1

Hi I want to set color to particular element and write excel/html in pandas dataframe.

my data frame is :

  old                new            diff
  query result       query result   result
1 q1    a1           q1    a1       True
2 q2    a2           q2    a5       False
3 q3    a3           q3    a3       True
4 q4    a4           q4    a6       False

I want to highlight "False" data in ['diff']['result'] column when I write this dataframe to excel/html.

How can I highlighting cells?

Thanks

gehbiszumeis
  • 3,525
  • 4
  • 24
  • 41
sjpark
  • 111
  • 9

2 Answers2

5

Change function highlight_max, use Styler.applymap and for select MultiIndex column use tuple:

def coloring(val):
    color = 'red' if val is False else ''
    return 'color: %s' % color

df.style.applymap(coloring, subset=[('diff','result')])
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
1

Here's what you could do.

def color_false_red(val):
    color = 'red' if val is False else 'black'
    return 'color: %s' % color

and in your df

df.style.apply(color_false_red, subset=['diff'])

This would change the color of the text.

Vishakha Lall
  • 1,178
  • 12
  • 33