pandas.io.formats.style.Styler.set_table_styles#

Styler.set_table_styles(table_styles=None, axis=0, overwrite=True, css_class_names=None)[source]#

Set the table styles included within the <style> HTML element.

This function can be used to style the entire table, columns, rows or specific HTML selectors.

Parameters:
table_styleslist or dict

If supplying a list, each individual table_style should be a dictionary with selector and props keys. selector should be a CSS selector that the style will be applied to (automatically prefixed by the table’s UUID) and props should be a list of tuples with (attribute, value). If supplying a dict, the dict keys should correspond to column names or index values, depending upon the specified axis argument. These will be mapped to row or col CSS selectors. MultiIndex values as dict keys should be in their respective tuple form. The dict values should be a list as specified in the form with CSS selectors and props that will be applied to the specified row or column.

axis{0 or ‘index’, 1 or ‘columns’, None}, default 0

Apply to each column (axis=0 or 'index'), to each row (axis=1 or 'columns'). Only used if table_styles is dict.

overwritebool, default True

Styles are replaced if True, or extended if False. CSS rules are preserved so most recent styles set will dominate if selectors intersect.

css_class_namesdict, optional

A dict of strings used to replace the default CSS classes described below.

Added in version 1.4.0.

Returns:
Styler

See also

Styler.set_td_classes

Set the DataFrame of strings added to the class attribute of <td> HTML elements.

Styler.set_table_attributes

Set the table attributes added to the <table> HTML element.

Notes

The default CSS classes dict, whose values can be replaced is as follows:

css_class_names = {
    "row_heading": "row_heading",
    "col_heading": "col_heading",
    "index_name": "index_name",
    "col": "col",
    "row": "row",
    "col_trim": "col_trim",
    "row_trim": "row_trim",
    "level": "level",
    "data": "data",
    "blank": "blank",
    "foot": "foot",
}

Examples

>>> df = pd.DataFrame(np.random.randn(10, 4), columns=["A", "B", "C", "D"])
>>> df.style.set_table_styles(
...     [{"selector": "tr:hover", "props": [("background-color", "yellow")]}]
... )  

Or with CSS strings

>>> df.style.set_table_styles(
...     [
...         {
...             "selector": "tr:hover",
...             "props": "background-color: yellow; font-size: 1em;",
...         }
...     ]
... )  

Adding column styling by name

>>> df.style.set_table_styles(
...     {
...         "A": [{"selector": "", "props": [("color", "red")]}],
...         "B": [{"selector": "td", "props": "color: blue;"}],
...     },
...     overwrite=False,
... )  

Adding row styling

>>> df.style.set_table_styles(
...     {0: [{"selector": "td:hover", "props": [("font-size", "25px")]}]},
...     axis=1,
...     overwrite=False,
... )  

See Table Visualization user guide for more details.