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

The Great
  • 7,215
  • 7
  • 40
  • 128
  • It is the same case that https://stackoverflow.com/questions/47469478/how-to-color-whole-rows-in-python-pandas-based-on-value – jpg997 May 19 '22 at 09:56
  • @jpg997 - but do you know whether it will retain format when we convert to html table? Thats why didnt use style – The Great May 19 '22 at 10:35

1 Answers1

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

Ynjxsjmh
  • 28,441
  • 6
  • 34
  • 52
  • @TheGreat Updated the answer. `output.html` contains the table in picture. – Ynjxsjmh May 19 '22 at 10:36
  • 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 – The Great May 19 '22 at 10:58
  • @TheGreat Have never used that package. – Ynjxsjmh May 19 '22 at 11:27
  • when I do `s.to_html`, I get an error like ` TypeError: '>' not supported between instances of 'str' and 'int'` – The Great May 23 '22 at 06:03
  • @TheGreat Couldn't reproduce the error, can you try replace `row['revenue']` with `float(row['revenue'])`? – Ynjxsjmh May 23 '22 at 06:11
  • let's say my revenue has values in terms of percentage like 234% (with pct symbol), do you know how can we compare? I guess thats why it is throwing error – The Great May 23 '22 at 06:18
  • @TheGreat That's the reason, you should convert `revenue` column to float type first. – Ynjxsjmh May 23 '22 at 06:19
  • I solved it but one minor issue. why is that your table doesn't have full border? meaning, your border lines are white. How can we make it black? – The Great May 23 '22 at 06:31
  • @TheGreat Maybe you can try https://stackoverflow.com/questions/64812819/pandas-dataframe-style-lost-table-border – Ynjxsjmh May 23 '22 at 08:04