Search

Provisional: This is a new feature and still under development. We'll be adding features and possibly making breaking changes in future releases. We'd love to hear your feedback.

This document is written as a Jupyter Notebook, and can be viewed or downloaded here.

You can apply conditional formatting, the visual styling of a DataFrame
depending on the data within, by using the DataFrame.style property.
This is a property that returns a pandas.Styler object, which has
useful methods for formatting and displaying DataFrames.

The styling is accomplished using CSS.
You write "style functions" that take scalars, DataFrames or Series, and return like-indexed DataFrames or Series with CSS "attribute: value" pairs for the values.
These functions can be incrementally passed to the Styler which collects the styles before rendering.

Both of those methods take a function (and some other keyword arguments) and applies your function to the DataFrame in a certain way.
Styler.applymap works through the DataFrame elementwise.
Styler.apply passes each column or row into your DataFrame one-at-a-time or the entire table at once, depending on the axis keyword argument.
For columnwise use axis=0, rowwise use axis=1, and for the entire table at once use axis=None.

The result of the function application, a CSS attribute-value pair, is stored in an internal dictionary on your Styler object.

Here's a boring example of rendering a DataFrame, without any (visible) styles:

In [2]:

df.style

Out[2]:

A

B

C

D

E

None

0

1

1.32921

nan

-0.31628

-0.99081

1

2

-1.07082

-1.43871

0.564417

0.295722

2

3

-1.6264

0.219565

0.678805

1.88927

3

4

0.961538

0.104011

-0.481165

0.850229

4

5

1.45342

1.05774

0.165562

0.515018

5

6

-1.33694

0.562861

1.39285

-0.063328

6

7

0.121668

1.2076

-0.00204021

1.6278

7

8

0.354493

1.03753

-0.385684

0.519818

8

9

1.68658

-1.32596

1.42898

-2.08935

9

10

-0.12982

0.631523

-0.586538

0.29072

Note: The DataFrame.style attribute is a propetry that returns a Styler object. Styler has a _repr_html_ method defined on it so they are rendered automatically. If you want the actual HTML back for further processing or for writing to file call the .render() method which returns a string.

The above output looks very similar to the standard DataFrame HTML representation. But we've done some work behind the scenes to attach CSS classes to each cell. We can view these by calling the .render method.

The row0_col2 is the identifier for that particular cell. We've also prepended each row/column identifier with a UUID unique to each DataFrame so that the style from one doesn't collide with the styling from another within the same notebook or page (you can set the uuid if you'd like to tie together the styling of two DataFrames).

When writing style functions, you take care of producing the CSS attribute / value pairs you want. Pandas matches those up with the CSS classes that identify each cell.

In this case, the cell's style depends only on it's own value.
That means we should use the Styler.applymap method which works elementwise.

In [5]:

s=df.style.applymap(color_negative_red)s

Out[5]:

A

B

C

D

E

None

0

1

1.32921

nan

-0.31628

-0.99081

1

2

-1.07082

-1.43871

0.564417

0.295722

2

3

-1.6264

0.219565

0.678805

1.88927

3

4

0.961538

0.104011

-0.481165

0.850229

4

5

1.45342

1.05774

0.165562

0.515018

5

6

-1.33694

0.562861

1.39285

-0.063328

6

7

0.121668

1.2076

-0.00204021

1.6278

7

8

0.354493

1.03753

-0.385684

0.519818

8

9

1.68658

-1.32596

1.42898

-2.08935

9

10

-0.12982

0.631523

-0.586538

0.29072

Notice the similarity with the standard df.applymap, which operates on DataFrames elementwise. We want you to be able to resuse your existing knowledge of how to interact with DataFrames.

Notice also that our function returned a string containing the CSS attribute and value, separated by a colon just like in a <style> tag. This will be a common theme.

Now suppose you wanted to highlight the maximum value in each column.
We can't use .applymap anymore since that operated elementwise.
Instead, we'll turn to .apply which operates columnwise (or rowwise using the axis keyword). Later on we'll see that something like highlight_max is already defined on Styler so you wouldn't need to write this yourself.

In [6]:

defhighlight_max(s):''' highlight the maximum in a Series yellow. '''is_max=s==s.max()return['background-color: yellow'ifvelse''forvinis_max]

In [7]:

df.style.apply(highlight_max)

Out[7]:

A

B

C

D

E

None

0

1

1.32921

nan

-0.31628

-0.99081

1

2

-1.07082

-1.43871

0.564417

0.295722

2

3

-1.6264

0.219565

0.678805

1.88927

3

4

0.961538

0.104011

-0.481165

0.850229

4

5

1.45342

1.05774

0.165562

0.515018

5

6

-1.33694

0.562861

1.39285

-0.063328

6

7

0.121668

1.2076

-0.00204021

1.6278

7

8

0.354493

1.03753

-0.385684

0.519818

8

9

1.68658

-1.32596

1.42898

-2.08935

9

10

-0.12982

0.631523

-0.586538

0.29072

We encourage you to use method chains to build up a style piecewise, before finally rending at the end of the chain.

In [8]:

df.style.\
applymap(color_negative_red).\
apply(highlight_max)

Out[8]:

A

B

C

D

E

None

0

1

1.32921

nan

-0.31628

-0.99081

1

2

-1.07082

-1.43871

0.564417

0.295722

2

3

-1.6264

0.219565

0.678805

1.88927

3

4

0.961538

0.104011

-0.481165

0.850229

4

5

1.45342

1.05774

0.165562

0.515018

5

6

-1.33694

0.562861

1.39285

-0.063328

6

7

0.121668

1.2076

-0.00204021

1.6278

7

8

0.354493

1.03753

-0.385684

0.519818

8

9

1.68658

-1.32596

1.42898

-2.08935

9

10

-0.12982

0.631523

-0.586538

0.29072

Above we used Styler.apply to pass in each column one at a time.

*Debugging Tip*: If you're having trouble writing your style function, try just passing it into DataFrame.apply. Internally, Styler.apply uses DataFrame.apply so the result should be the same.

What if you wanted to highlight just the maximum value in the entire table?
Use .apply(function, axis=None) to indicate that your function wants the entire table, not one column or row at a time. Let's try that next.

We'll rewrite our highlight-max to handle either Series (from .apply(axis=0 or 1)) or DataFrames (from .apply(axis=None)). We'll also allow the color to be adjustable, to demonstrate that .apply, and .applymap pass along keyword arguments.

In [9]:

defhighlight_max(data,color='yellow'):''' highlight the maximum in a Series or DataFrame '''attr='background-color: {}'.format(color)ifdata.ndim==1:# Series from .apply(axis=0) or axis=1is_max=data==data.max()return[attrifvelse''forvinis_max]else:# from .apply(axis=None)is_max=data==data.max().max()returnpd.DataFrame(np.where(is_max,attr,''),index=data.index,columns=data.columns)

We distinguish the display value from the actual value in Styler.
To control the display value, the text is printed in each cell, use Styler.format. Cells can be formatted according to a format spec string or a callable that takes a single value and returns a string.

In [13]:

df.style.format("{:.2%}")

Out[13]:

A

B

C

D

E

None

0

100.00%

132.92%

nan%

-31.63%

-99.08%

1

200.00%

-107.08%

-143.87%

56.44%

29.57%

2

300.00%

-162.64%

21.96%

67.88%

188.93%

3

400.00%

96.15%

10.40%

-48.12%

85.02%

4

500.00%

145.34%

105.77%

16.56%

51.50%

5

600.00%

-133.69%

56.29%

139.29%

-6.33%

6

700.00%

12.17%

120.76%

-0.20%

162.78%

7

800.00%

35.45%

103.75%

-38.57%

51.98%

8

900.00%

168.66%

-132.60%

142.90%

-208.94%

9

1000.00%

-12.98%

63.15%

-58.65%

29.07%

Use a dictionary to format specific columns.

In [14]:

df.style.format({'B':"{:0<4.0f}",'D':'{:+.2f}'})

Out[14]:

A

B

C

D

E

None

0

1

1000

nan

-0.32

-0.99081

1

2

-100

-1.43871

+0.56

0.295722

2

3

-200

0.219565

+0.68

1.88927

3

4

1000

0.104011

-0.48

0.850229

4

5

1000

1.05774

+0.17

0.515018

5

6

-100

0.562861

+1.39

-0.063328

6

7

0000

1.2076

-0.00

1.6278

7

8

0000

1.03753

-0.39

0.519818

8

9

2000

-1.32596

+1.43

-2.08935

9

10

-000

0.631523

-0.59

0.29072

Or pass in a callable (or dictionary of callables) for more flexible handling.

Styler.background_gradient takes the keyword arguments low and high. Roughly speaking these extend the range of your data by low and high percent so that when we convert the colors, the colormap's entire range isn't used. This is useful so that you can actually read the text still.

In [18]:

# Uses the full color rangedf.loc[:4].style.background_gradient(cmap='viridis')

Say you have a lovely style built up for a DataFrame, and now you want to apply the same style to a second DataFrame. Export the style with df1.style.export, and import it on the second DataFrame with df1.style.set

In [24]:

df2=-dfstyle1=df.style.applymap(color_negative_red)style1

Out[24]:

A

B

C

D

E

None

0

1

1.32921

nan

-0.31628

-0.99081

1

2

-1.07082

-1.43871

0.564417

0.295722

2

3

-1.6264

0.219565

0.678805

1.88927

3

4

0.961538

0.104011

-0.481165

0.850229

4

5

1.45342

1.05774

0.165562

0.515018

5

6

-1.33694

0.562861

1.39285

-0.063328

6

7

0.121668

1.2076

-0.00204021

1.6278

7

8

0.354493

1.03753

-0.385684

0.519818

8

9

1.68658

-1.32596

1.42898

-2.08935

9

10

-0.12982

0.631523

-0.586538

0.29072

In [25]:

style2=df2.stylestyle2.use(style1.export())style2

Out[25]:

A

B

C

D

E

None

0

-1

-1.32921

nan

0.31628

0.99081

1

-2

1.07082

1.43871

-0.564417

-0.295722

2

-3

1.6264

-0.219565

-0.678805

-1.88927

3

-4

-0.961538

-0.104011

0.481165

-0.850229

4

-5

-1.45342

-1.05774

-0.165562

-0.515018

5

-6

1.33694

-0.562861

-1.39285

0.063328

6

-7

-0.121668

-1.2076

0.00204021

-1.6278

7

-8

-0.354493

-1.03753

0.385684

-0.519818

8

-9

-1.68658

1.32596

-1.42898

2.08935

9

-10

0.12982

-0.631523

0.586538

-0.29072

Notice that you're able share the styles even though they're data aware. The styles are re-evaluated on the new DataFrame they've been used upon.

You've seen a few methods for data-driven styling.
Styler also provides a few other options for styles that don't depend on the data.

precision

captions

table-wide styles

Each of these can be specified in two ways:

A keyword argument to pandas.core.Styler

A call to one of the .set_ methods, e.g. .set_caption

The best method to use depends on the context. Use the Styler constructor when building many styled DataFrames that should all share the same properties. For interactive use, the.set_ methods are more convenient.

Setting the precision only affects the printed number; the full-precision values are always passed to your style functions. You can always use df.round(2).style if you'd prefer to round from the start.

The next option you have are "table styles".
These are styles that apply to the table as a whole, but don't look at the data.
Certain sytlings, including pseudo-selectors like :hover can only be used this way.

In [29]:

fromIPython.displayimportHTMLdefhover(hover_color="#ffff99"):returndict(selector="tr:hover",props=[("background-color","%s"%hover_color)])styles=[hover(),dict(selector="th",props=[("font-size","150%"),("text-align","center")]),dict(selector="caption",props=[("caption-side","bottom")])]html=(df.style.set_table_styles(styles).set_caption("Hover to highlight."))html

Out[29]:

Hover to highlight.

A

B

C

D

E

None

0

1

1.32921

nan

-0.31628

-0.99081

1

2

-1.07082

-1.43871

0.564417

0.295722

2

3

-1.6264

0.219565

0.678805

1.88927

3

4

0.961538

0.104011

-0.481165

0.850229

4

5

1.45342

1.05774

0.165562

0.515018

5

6

-1.33694

0.562861

1.39285

-0.063328

6

7

0.121668

1.2076

-0.00204021

1.6278

7

8

0.354493

1.03753

-0.385684

0.519818

8

9

1.68658

-1.32596

1.42898

-2.08935

9

10

-0.12982

0.631523

-0.586538

0.29072

table_styles should be a list of dictionaries.
Each dictionary should have the selector and props keys.
The value for selector should be a valid CSS selector.
Recall that all the styles are already attached to an id, unique to
each Styler. This selector is in addition to that id.
The value for props should be a list of tuples of ('attribute', 'value').

table_styles are extremely flexible, but not as fun to type out by hand.
We hope to collect some useful ones either in pandas, or preferable in a new package that builds on top the tools here.

table style: a dictionary with the two keys selector and props. selector is the CSS selector that props will apply to. props is a list of (attribute, value) tuples. A list of table styles passed into Styler.

This section contains a bit of information about the implementation of Styler.
Since the feature is so new all of this is subject to change, even more so than the end-use API.

As users apply styles (via .apply, .applymap or one of the builtins), we don't actually calculate anything.
Instead, we append functions and arguments to a list self._todo.
When asked (typically in .render we'll walk through the list and execute each function (this is in self._compute().
These functions update an internal defaultdict(list), self.ctx which maps DataFrame row / column positions to CSS attribute, value pairs.

We take the extra step through self._todo so that we can export styles and set them on other Stylers.

Rendering uses Jinja templates.
The .translate method takes self.ctx and builds another dictionary ready to be passed into Styler.template.render, the Jinja template.