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

1

2 Answers 2

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')])
Sign up to request clarification or add additional context in comments.

7 Comments

And Is there a method to apply this function .to_html method?
@sjpark - no, but if want render html only add html = df.style.applymap(coloring, subset=[('diff','result')]).render()
@Ambleu - Sure, change color to background-color
how to include index as subset? and does it now work on multiple sheet excel?
and does it now work on multiple sheet excel - No, you need processing each sheet separately.
|
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.