Styling pandas dataframe to show negative numbers in red

All we need is an easy explanation of the problem, so here it is.

I am trying to following the instructions here to apply styling to a pandas dataframe.

However I can’t understand how to combine the elements of

set_table_styles()

and

df.style.applymap()

Here is my code:

df = pd.DataFrame([[1,2,3,4], [5,6,7,8], [3,-5,6,7]], columns=['Foo', 'Bar', 'Buzz', 'Fuzz'], index=['First', 'Second','Third'])

I can apply the negative red coloring:

def style_negative(v, props=''):
    return props if v < 0 else None

df.style.applymap(style_negative, props='color:red;')

Which works as expected:
Styling pandas dataframe to show negative numbers in red

And here is my styling code:

styles = [
    dict(selector="tr:hover",
                props=[("background", "#D6EEEE")]),
    dict(selector="th", props=[("color", "#fff"),
                               ("border", "1px solid #eee"),
                               ("padding", "12px 35px"),
                               ("border-collapse", "collapse"),
                               ("background", "#1D4477"),                             
                               ("font-size", "18px")
                               ]),
    dict(selector="td", props=[("color", "#000000"),
                               ("border", "1px solid #eee"),
                               ("padding", "12px 35px"),
                               ("border-collapse", "collapse"),
                               ("font-size", "15px")
                               ]),
    dict(selector="table", props=[                                   
                                    ("font-family" , 'Helvetica'),
                                    ("margin" , "25px auto"),
                                    ("border-collapse" , "collapse"),
                                    ("border" , "1px solid #eee"),
                                    ("border-bottom" , "2px solid #00cccc"),                                    
                                      ]),
    dict(selector="caption", props=[("caption-side", "bottom")]),
    dict(selector="tr:nth-child(even)", props=[
        ("background-color", "#f2f2f2"),
    ]),
]
df = df.set_table_styles(styles).format(precision=2)

Which gives:
Styling pandas dataframe to show negative numbers in red

But I don’t know how to combine the two, such that I get the table styling as well as the negative numbers showing as red.

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

You can chain both methods, but first remove color parameter for td elements like mentioned @Attack68
:

styles = [
    dict(selector="tr:hover",
                props=[("background", "#D6EEEE")]),
    dict(selector="th", props=[("color", "#fff"),
                               ("border", "1px solid #eee"),
                               ("padding", "12px 35px"),
                               ("border-collapse", "collapse"),
                               ("background", "#1D4477"),                             
                               ("font-size", "18px")
                               ]),
    dict(selector="td", props=[("border", "1px solid #eee"),
                               ("padding", "12px 35px"),
                               ("border-collapse", "collapse"),
                               ("font-size", "15px")
                               ]),
    dict(selector="table", props=[                                   
                                    ("font-family" , 'Helvetica'),
                                    ("margin" , "25px auto"),
                                    ("border-collapse" , "collapse"),
                                    ("border" , "1px solid #eee"),
                                    ("border-bottom" , "2px solid #00cccc"),                                    
                                      ]),
    dict(selector="caption", props=[("caption-side", "bottom")]),
    dict(selector="tr:nth-child(even)", props=[
        ("background-color", "#f2f2f2"),
    ]),
]

(df.style.applymap(style_negative, props='color:red;').set_table_styles(styles))

(df.style.applymap(style_negative, props='color:red;')
         .set_table_styles(styles))

Styling pandas dataframe to show negative numbers in red

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply