machinelearner07
machinelearner07

Reputation: 113

Conditionally format dataframe highlighting

I was wondering if there is a good way to format the column titles in pandas as well as certain column values in order to higlight values in a column.I will eventually be exporting to excel, but it would be nice if I could do the formatting before that step. Here's a simple example dataframe. Let me know if there are any good packages and tips to using them would be appreciated.

Say I wanted to highlight any cells in column 2 that are negative. Also, if there is a way to fill the white space behind the column titles and make them bold that would be great.

COL1  COL2  COL3
0     a    -2
1     a     4
2     b     10
3     b     -8
4     a     10
5     b     5

Upvotes: 1

Views: 48

Answers (1)

Supratim Haldar
Supratim Haldar

Reputation: 2416

Try something like this.

def highlight(value):
    if value < 0:
        return ['background-color: yellow']

df.style.applymap(highlight)

You'll get more examples here for reference - https://pandas.pydata.org/pandas-docs/stable/user_guide/style.html.

Upvotes: 1

Related Questions