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 Styler object, which has useful methods for formatting and displaying DataFrames.
DataFrame.style
Styler
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.
DataFrame
Series
"attribute: value"
Pass your style functions into one of the following methods:
Styler.applymap: elementwise
Styler.applymap
Styler.apply: column-/row-/table-wise
Styler.apply
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.
axis
axis=0
axis=1
axis=None
For Styler.applymap your function should take a scalar and return a single string with the CSS attribute-value pair.
For Styler.apply your function should take a Series or DataFrame (depending on the axis parameter), and return a Series or DataFrame with an identical shape where each value is a string with a CSS attribute-value pair.
Let’s see some examples.
[2]:
import pandas as pd import numpy as np np.random.seed(24) df = pd.DataFrame({'A': np.linspace(1, 10, 10)}) df = pd.concat([df, pd.DataFrame(np.random.randn(10, 4), columns=list('BCDE'))], axis=1) df.iloc[3, 3] = np.nan df.iloc[0, 2] = np.nan
Here’s a boring example of rendering a DataFrame, without any (visible) styles:
[3]:
df.style
Note: The DataFrame.style attribute is a property 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.
_repr_html_
.render()
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.
.render
[4]:
df.style.highlight_null().render().split('\n')[:10]
['<style type="text/css" >', ' #T_5286f570_3b71_11ea_b67d_9cb6d0992cf9row0_col2 {', ' background-color: red;', ' } #T_5286f570_3b71_11ea_b67d_9cb6d0992cf9row3_col3 {', ' background-color: red;', ' }</style><table id="T_5286f570_3b71_11ea_b67d_9cb6d0992cf9" ><thead> <tr> <th class="blank level0" ></th> <th class="col_heading level0 col0" >A</th> <th class="col_heading level0 col1" >B</th> <th class="col_heading level0 col2" >C</th> <th class="col_heading level0 col3" >D</th> <th class="col_heading level0 col4" >E</th> </tr></thead><tbody>', ' <tr>', ' <th id="T_5286f570_3b71_11ea_b67d_9cb6d0992cf9level0_row0" class="row_heading level0 row0" >0</th>', ' <td id="T_5286f570_3b71_11ea_b67d_9cb6d0992cf9row0_col0" class="data row0 col0" >1.000000</td>', ' <td id="T_5286f570_3b71_11ea_b67d_9cb6d0992cf9row0_col1" class="data row0 col1" >1.329212</td>']
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).
row0_col2
uuid
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.
Let’s write a simple style function that will color negative numbers red and positive numbers black.
[5]:
def color_negative_red(val): """ Takes a scalar and returns a string with the css property `'color: red'` for negative strings, black otherwise. """ color = 'red' if val < 0 else 'black' return 'color: %s' % color
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.
[6]:
s = df.style.applymap(color_negative_red) s
Notice the similarity with the standard df.applymap, which operates on DataFrames elementwise. We want you to be able to reuse your existing knowledge of how to interact with DataFrames.
df.applymap
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.
<style>
Finally, the input shapes matched. Styler.applymap calls the function on each scalar input, and the function returns a scalar output.
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.
.applymap
.apply
highlight_max
[7]:
def highlight_max(s): ''' highlight the maximum in a Series yellow. ''' is_max = s == s.max() return ['background-color: yellow' if v else '' for v in is_max]
[8]:
df.style.apply(highlight_max)
In this case the input is a Series, one column at a time. Notice that the output shape of highlight_max matches the input shape, an array with len(s) items.
len(s)
We encourage you to use method chains to build up a style piecewise, before finally rending at the end of the chain.
[9]:
df.style.\ applymap(color_negative_red).\ apply(highlight_max)
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.
.apply(function, axis=None)
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.
highlight-max
.apply(axis=0 or 1)
.apply(axis=None)
[10]:
def highlight_max(data, color='yellow'): ''' highlight the maximum in a Series or DataFrame ''' attr = 'background-color: {}'.format(color) if data.ndim == 1: # Series from .apply(axis=0) or axis=1 is_max = data == data.max() return [attr if v else '' for v in is_max] else: # from .apply(axis=None) is_max = data == data.max().max() return pd.DataFrame(np.where(is_max, attr, ''), index=data.index, columns=data.columns)
When using Styler.apply(func, axis=None), the function must return a DataFrame with the same index and column labels.
Styler.apply(func, axis=None)
[11]:
df.style.apply(highlight_max, color='darkorange', axis=None)
Style functions should return strings with one or more CSS attribute: value delimited by semicolons. Use
attribute: value
Styler.applymap(func) for elementwise styles
Styler.applymap(func)
Styler.apply(func, axis=0) for columnwise styles
Styler.apply(func, axis=0)
Styler.apply(func, axis=1) for rowwise styles
Styler.apply(func, axis=1)
Styler.apply(func, axis=None) for tablewise styles
And crucially the input and output shapes of func must match. If x is the input then func(x).shape == x.shape.
func
x
func(x).shape == x.shape
Both Styler.apply, and Styler.applymap accept a subset keyword. This allows you to apply styles to specific rows or columns, without having to code that logic into your style function.
subset
style
The value passed to subset behaves similar to slicing a DataFrame.
A scalar is treated as a column label
A list (or series or numpy array)
A tuple is treated as (row_indexer, column_indexer)
(row_indexer, column_indexer)
Consider using pd.IndexSlice to construct the tuple for the last one.
pd.IndexSlice
[12]:
df.style.apply(highlight_max, subset=['B', 'C', 'D'])
For row and column slicing, any valid indexer to .loc will work.
.loc
[13]:
df.style.applymap(color_negative_red, subset=pd.IndexSlice[2:5, ['B', 'D']])
Only label-based slicing is supported right now, not positional.
If your style function uses a subset or axis keyword argument, consider wrapping your function in a functools.partial, partialing out that keyword.
functools.partial
my_func2 = functools.partial(my_func, subset=42)
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.
Styler.format
[14]:
df.style.format("{:.2%}")
Use a dictionary to format specific columns.
[15]:
df.style.format({'B': "{:0<4.0f}", 'D': '{:+.2f}'})
Or pass in a callable (or dictionary of callables) for more flexible handling.
[16]:
df.style.format({"B": lambda x: "±{:.2f}".format(abs(x))})
You can format the text displayed for missing values by na_rep.
na_rep
[17]:
df.style.format("{:.2%}", na_rep="-")
These formatting techniques can be used in combination with styling.
[18]:
df.style.highlight_max().format(None, na_rep="-")
Finally, we expect certain styling functions to be common enough that we’ve included a few “built-in” to the Styler, so you don’t have to write them yourself.
[19]:
df.style.highlight_null(null_color='red')
You can create “heatmaps” with the background_gradient method. These require matplotlib, and we’ll use Seaborn to get a nice colormap.
background_gradient
[20]:
import seaborn as sns cm = sns.light_palette("green", as_cmap=True) s = df.style.background_gradient(cmap=cm) s
--------------------------------------------------------------------------- ModuleNotFoundError Traceback (most recent call last) <ipython-input-20-598ad5eeb200> in <module> ----> 1 import seaborn as sns 2 3 cm = sns.light_palette("green", as_cmap=True) 4 5 s = df.style.background_gradient(cmap=cm) ModuleNotFoundError: No module named 'seaborn'
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.
Styler.background_gradient
low
high
[21]:
# Uses the full color range df.loc[:4].style.background_gradient(cmap='viridis')
/home/joris/miniconda3/envs/docs/lib/python3.7/site-packages/matplotlib/colors.py:479: RuntimeWarning: invalid value encountered in less xa[xa < 0] = -1
[22]:
# Compress the color range (df.loc[:4] .style .background_gradient(cmap='viridis', low=.5, high=0) .highlight_null('red'))
There’s also .highlight_min and .highlight_max.
.highlight_min
.highlight_max
[23]:
df.style.highlight_max(axis=0)
Use Styler.set_properties when the style doesn’t actually depend on the values.
Styler.set_properties
[24]:
df.style.set_properties(**{'background-color': 'black', 'color': 'lawngreen', 'border-color': 'white'})
You can include “bar charts” in your DataFrame.
[25]:
df.style.bar(subset=['A', 'B'], color='#d65f5f')
New in version 0.20.0 is the ability to customize further the bar chart: You can now have the df.style.bar be centered on zero or midpoint value (in addition to the already existing way of having the min value at the left side of the cell), and you can pass a list of [color_negative, color_positive].
df.style.bar
[color_negative, color_positive]
Here’s how you can change the above with the new align='mid' option:
align='mid'
[26]:
df.style.bar(subset=['A', 'B'], align='mid', color=['#d65f5f', '#5fba7d'])
The following example aims to give a highlight of the behavior of the new align options:
[27]:
import pandas as pd from IPython.display import HTML # Test series test1 = pd.Series([-100,-60,-30,-20], name='All Negative') test2 = pd.Series([10,20,50,100], name='All Positive') test3 = pd.Series([-10,-5,0,90], name='Both Pos and Neg') head = """ <table> <thead> <th>Align</th> <th>All Negative</th> <th>All Positive</th> <th>Both Neg and Pos</th> </thead> </tbody> """ aligns = ['left','zero','mid'] for align in aligns: row = "<tr><th>{}</th>".format(align) for serie in [test1,test2,test3]: s = serie.copy() s.name='' row += "<td>{}</td>".format(s.to_frame().style.bar(align=align, color=['#d65f5f', '#5fba7d'], width=100).render()) #testn['width'] row += '</tr>' head += row head+= """ </tbody> </table>""" HTML(head)
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
df1.style.export
df1.style.set
[28]:
df2 = -df style1 = df.style.applymap(color_negative_red) style1
[29]:
style2 = df2.style style2.use(style1.export()) style2
Notice that you’re able to share the styles even though they’re data aware. The styles are re-evaluated on the new DataFrame they’ve been used upon.
use
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
missing values representation
hiding the index or columns
Each of these can be specified in two ways:
A keyword argument to Styler.__init__
Styler.__init__
A call to one of the .set_ or .hide_ methods, e.g. .set_caption or .hide_columns
.set_
.hide_
.set_caption
.hide_columns
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_ and .hide_ methods are more convenient.
You can control the precision of floats using pandas’ regular display.precision option.
display.precision
[30]:
with pd.option_context('display.precision', 2): html = (df.style .applymap(color_negative_red) .apply(highlight_max)) html
Or through a set_precision method.
set_precision
[31]:
df.style\ .applymap(color_negative_red)\ .apply(highlight_max)\ .set_precision(2)
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.
df.round(2).style
Regular table captions can be added in a few ways.
[32]:
df.style.set_caption('Colormaps, with a caption.')\ .background_gradient(cmap=cm)
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-32-442d93958330> in <module> 1 df.style.set_caption('Colormaps, with a caption.')\ ----> 2 .background_gradient(cmap=cm) NameError: name 'cm' is not defined
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.
:hover
[33]:
from IPython.display import HTML def hover(hover_color="#ffff99"): return dict(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
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
selector
props
id
('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.
You can control the default missing values representation for the entire table through set_na_rep method.
set_na_rep
[34]:
(df.style .set_na_rep("FAIL") .format(None, na_rep="PASS", subset=["D"]) .highlight_null("yellow"))
The index can be hidden from rendering by calling Styler.hide_index. Columns can be hidden from rendering by calling Styler.hide_columns and passing in the name of a column, or a slice of columns.
Styler.hide_index
Styler.hide_columns
[35]:
df.style.hide_index()
[36]:
df.style.hide_columns(['C','D'])
Certain CSS classes are attached to cells.
Index and Column names include index_name and level<k> where k is its level in a MultiIndex
index_name
level<k>
k
Index label cells include
row_heading
row<n> where n is the numeric position of the row
row<n>
n
level<k> where k is the level in a MultiIndex
Column label cells include
col_heading
col<n> where n is the numeric position of the column
col<n>
Blank cells include blank
blank
Data cells include data
data
DataFrame only (use Series.to_frame().style)
(use Series.to_frame().style)
The index and columns must be unique
No large repr, and performance isn’t great; this is intended for summary DataFrames
You can only style the values, not the index or columns
You can only apply styles, you can’t insert new HTML entities
Some of these will be addressed in the future.
Style function: a function that’s passed into Styler.apply or Styler.applymap and returns values like 'css attribute: value'
'css attribute: value'
Builtin style functions: style functions that are methods on Styler
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.
(attribute, value)
Here are a few interesting examples.
Styler interacts pretty well with widgets. If you’re viewing this online instead of running the notebook yourself, you’re missing out on interactively adjusting the color palette.
[37]:
from IPython.html import widgets @widgets.interact def f(h_neg=(0, 359, 1), h_pos=(0, 359), s=(0., 99.9), l=(0., 99.9)): return df.style.background_gradient( cmap=sns.palettes.diverging_palette(h_neg=h_neg, h_pos=h_pos, s=s, l=l, as_cmap=True) )
/home/joris/miniconda3/envs/docs/lib/python3.7/site-packages/IPython/html.py:14: ShimWarning: The `IPython.html` package has been deprecated since IPython 4.0. You should import from `notebook` instead. `IPython.html.widgets` has moved to `ipywidgets`. "`IPython.html.widgets` has moved to `ipywidgets`.", ShimWarning)
--------------------------------------------------------------------------- ModuleNotFoundError Traceback (most recent call last) ~/miniconda3/envs/docs/lib/python3.7/site-packages/IPython/utils/shimmodule.py in __getattr__(self, key) 91 try: ---> 92 return import_item(name) 93 except ImportError: ~/miniconda3/envs/docs/lib/python3.7/site-packages/IPython/utils/importstring.py in import_item(name) 30 package, obj = parts ---> 31 module = __import__(package, fromlist=[obj]) 32 try: ModuleNotFoundError: No module named 'ipywidgets' During handling of the above exception, another exception occurred: AttributeError Traceback (most recent call last) <ipython-input-37-2832dcd4da26> in <module> 1 from IPython.html import widgets ----> 2 @widgets.interact 3 def f(h_neg=(0, 359, 1), h_pos=(0, 359), s=(0., 99.9), l=(0., 99.9)): 4 return df.style.background_gradient( 5 cmap=sns.palettes.diverging_palette(h_neg=h_neg, h_pos=h_pos, s=s, l=l, ~/miniconda3/envs/docs/lib/python3.7/site-packages/IPython/utils/shimmodule.py in __getattr__(self, key) 92 return import_item(name) 93 except ImportError: ---> 94 raise AttributeError(key) AttributeError: interact
[38]:
def magnify(): return [dict(selector="th", props=[("font-size", "4pt")]), dict(selector="td", props=[('padding', "0em 0em")]), dict(selector="th:hover", props=[("font-size", "12pt")]), dict(selector="tr:hover td:hover", props=[('max-width', '200px'), ('font-size', '12pt')]) ]
[39]:
np.random.seed(25) cmap = cmap=sns.diverging_palette(5, 250, as_cmap=True) bigdf = pd.DataFrame(np.random.randn(20, 25)).cumsum() bigdf.style.background_gradient(cmap, axis=1)\ .set_properties(**{'max-width': '80px', 'font-size': '1pt'})\ .set_caption("Hover to magnify")\ .set_precision(2)\ .set_table_styles(magnify())
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-39-1e011daa671b> in <module> 1 np.random.seed(25) ----> 2 cmap = cmap=sns.diverging_palette(5, 250, as_cmap=True) 3 bigdf = pd.DataFrame(np.random.randn(20, 25)).cumsum() 4 5 bigdf.style.background_gradient(cmap, axis=1)\ NameError: name 'sns' is not defined
New in version 0.20.0
Experimental: 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.
Some support is available for exporting styled DataFrames to Excel worksheets using the OpenPyXL or XlsxWriter engines. CSS2.2 properties handled include:
DataFrames
OpenPyXL
XlsxWriter
background-color
border-style, border-width, border-color and their {top, right, bottom, left variants}
border-style
border-width
border-color
top
right
bottom
left
color
font-family
font-style
font-weight
text-align
text-decoration
vertical-align
white-space: nowrap
Only CSS2 named colors and hex colors of the form #rgb or #rrggbb are currently supported.
#rgb
#rrggbb
The following pseudo CSS properties are also available to set excel specific style properties:
number-format
[40]:
df.style.\ applymap(color_negative_red).\ apply(highlight_max).\ to_excel('styled.xlsx', engine='openpyxl')
--------------------------------------------------------------------------- ModuleNotFoundError Traceback (most recent call last) <ipython-input-40-a4812b06f625> in <module> 2 applymap(color_negative_red).\ 3 apply(highlight_max).\ ----> 4 to_excel('styled.xlsx', engine='openpyxl') ~/scipy/pandas/pandas/io/formats/style.py in to_excel(self, excel_writer, sheet_name, na_rep, float_format, columns, header, index, index_label, startrow, startcol, engine, merge_cells, encoding, inf_rep, verbose, freeze_panes) 233 startcol=startcol, 234 freeze_panes=freeze_panes, --> 235 engine=engine, 236 ) 237 ~/scipy/pandas/pandas/io/formats/excel.py in write(self, writer, sheet_name, startrow, startcol, freeze_panes, engine) 724 need_save = False 725 else: --> 726 writer = ExcelWriter(stringify_path(writer), engine=engine) 727 need_save = True 728 ~/scipy/pandas/pandas/io/excel/_openpyxl.py in __init__(self, path, engine, mode, **engine_kwargs) 16 def __init__(self, path, engine=None, mode="w", **engine_kwargs): 17 # Use the openpyxl module as the Excel writer. ---> 18 from openpyxl.workbook import Workbook 19 20 super().__init__(path, mode=mode, **engine_kwargs) ModuleNotFoundError: No module named 'openpyxl'
A screenshot of the output:
The core of pandas is, and will remain, its “high-performance, easy-to-use data structures”. With that in mind, we hope that DataFrame.style accomplishes two goals
Provide an API that is pleasing to use interactively and is “good enough” for many tasks
Provide the foundations for dedicated libraries to build on
If you build a great library on top of this, let us know and we’ll link to it.
If the default template doesn’t quite suit your needs, you can subclass Styler and extend or override the template. We’ll show an example of extending the default template to insert a custom header before each table.
[41]:
from jinja2 import Environment, ChoiceLoader, FileSystemLoader from IPython.display import HTML from pandas.io.formats.style import Styler
We’ll use the following template:
[42]:
with open("templates/myhtml.tpl") as f: print(f.read())
{% extends "html.tpl" %} {% block table %} <h1>{{ table_title|default("My Table") }}</h1> {{ super() }} {% endblock table %}
Now that we’ve created a template, we need to set up a subclass of Styler that knows about it.
[43]:
class MyStyler(Styler): env = Environment( loader=ChoiceLoader([ FileSystemLoader("templates"), # contains ours Styler.loader, # the default ]) ) template = env.get_template("myhtml.tpl")
Notice that we include the original loader in our environment’s loader. That’s because we extend the original template, so the Jinja environment needs to be able to find it.
Now we can use that custom styler. It’s __init__ takes a DataFrame.
__init__
[44]:
MyStyler(df)
Our custom template accepts a table_title keyword. We can provide the value in the .render method.
table_title
[45]:
HTML(MyStyler(df).render(table_title="Extending Example"))
For convenience, we provide the Styler.from_custom_template method that does the same as the custom subclass.
Styler.from_custom_template
[46]:
EasyStyler = Styler.from_custom_template("templates", "myhtml.tpl") EasyStyler(df)
Here’s the template structure:
[47]:
with open("templates/template_structure.html") as f: structure = f.read() HTML(structure)
<style type="text/css">
</style>
<table ...>
</table>
See the template in the GitHub repo for more details.