1

I have a dataframe like as below

test_id,status,revenue,cnt_days,age     
1,passed,234.54,3,21          
2,passed,543.21,5,29
11,failed,21.3,4,35
15,failed,2098.21,6,57             
51,passed,232,21,80     
75,failed,123.87,32,43

df1 = pd.read_clipboard(sep=',')

I would like to color the rows when revenue is greater than 500. So, I used pretty_html_table found here

So, I tried the below using conditions parameter but didnt work

build_table(data,'blue_light', font_size='8px',font_family='Open Sans,sans-serif',
                     text_align='center',width='70px',index=False, conditions={'Revenue': {'max':500,'max_color': 'red'}},even_color='black',even_bg_color='white')

But this didn't apply any color to the columns.

How can I use this to apply color, so I can use this html table in my email body?

I expect my output to be like as below with column header in yellow color and revenue > 500 rows in red color

image

2
  • It is the same case that stackoverflow.com/questions/47469478/… Commented May 19, 2022 at 9:56
  • @jpg997 - but do you know whether it will retain format when we convert to html table? Thats why didnt use style Commented May 19, 2022 at 10:35

1 Answer 1

3

You can try .style.apply and .set_table_styles

def highlight(row):
    if row['revenue'] > 500:
        return ['background-color: red'] * len(row)
    else:
        return [''] * len(row)

s = df.style.apply(highlight, axis=1)
s = s.set_table_styles([
    {
        'selector': '.col_heading',
        'props': 'background-color: yellow; color: black;'
    }
])

s.to_html('output.html')

enter image description here

Sign up to request clarification or add additional context in comments.

9 Comments

@TheGreat Updated the answer. output.html contains the table in picture.
can you try using build_table method that I have used from pretty html table package? I dont think it will work. I tried already
@TheGreat Have never used that package.
when I do s.to_html, I get an error like ` TypeError: '>' not supported between instances of 'str' and 'int'`
@TheGreat Couldn't reproduce the error, can you try replace row['revenue'] with float(row['revenue'])?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.