Table Visualization#

This section demonstrates visualization of tabular data using the Styler class. For information on visualization with charting please see Chart Visualization. This document is written as a Jupyter Notebook, and can be viewed or downloaded here.

Styler Object and Customising the Display#

Styling and output display customisation should be performed after the data in a DataFrame has been processed. The Styler is not dynamically updated if further changes to the DataFrame are made. The DataFrame.style attribute is a property that returns a Styler object. It has a _repr_html_ method defined on it so it is rendered automatically in Jupyter Notebook.

The Styler, which can be used for large data but is primarily designed for small data, currently has the ability to output to these formats:

  • HTML

  • LaTeX

  • String (and CSV by extension)

  • Excel

  • (JSON is not currently available)

The first three of these have display customisation methods designed to format and customise the output. These include:

  • Formatting values, the index and columns headers, using .format() and .format_index(),

  • Renaming the index or column header labels, using .relabel_index()

  • Hiding certain columns, the index and/or column headers, or index names, using .hide()

  • Concatenating similar DataFrames, using .concat()

Formatting the Display#

Formatting Values#

The Styler distinguishes the display value from the actual value, in both data values and index or columns headers. To control the display value, the text is printed in each cell as a string, and we can use the .format() and .format_index() methods to manipulate this according to a format spec string or a callable that takes a single value and returns a string. It is possible to define this for the whole table, or index, or for individual columns, or MultiIndex levels. We can also overwrite index names.

Additionally, the format function has a precision argument to specifically help format floats, as well as decimal and thousands separators to support other locales, an na_rep argument to display missing data, and an escape and hyperlinks arguments to help displaying safe-HTML or safe-LaTeX. The default formatter is configured to adopt pandas’ global options such as styler.format.precision option, controllable using with pd.option_context('format.precision', 2):

[1]:
import pandas as pd
import numpy as np

df = pd.DataFrame(
    {"strings": ["Adam", "Mike"], "ints": [1, 3], "floats": [1.123, 1000.23]}
)
df.style.format(precision=3, thousands=".", decimal=",").format_index(
    str.upper, axis=1
).relabel_index(["row 1", "row 2"], axis=0)
[1]:
  STRINGS INTS FLOATS
row 1 Adam 1 1,123
row 2 Mike 3 1.000,230

Using Styler to manipulate the display is a useful feature because maintaining the indexing and data values for other purposes gives greater control. You do not have to overwrite your DataFrame to display it how you like. Here is a more comprehensive example of using the formatting functions whilst still relying on the underlying data for indexing and calculations.

[2]:
weather_df = pd.DataFrame(
    np.random.default_rng().random((10, 2)) * 5,
    index=pd.date_range(start="2021-01-01", periods=10),
    columns=["Tokyo", "Beijing"],
)


def rain_condition(v):
    if v < 1.75:
        return "Dry"
    elif v < 2.75:
        return "Rain"
    return "Heavy Rain"


def make_pretty(styler):
    styler.set_caption("Weather Conditions")
    styler.format(rain_condition)
    styler.format_index(lambda v: v.strftime("%A"))
    styler.background_gradient(axis=None, vmin=1, vmax=5, cmap="YlGnBu")
    return styler


weather_df
[2]:
Tokyo Beijing
2021-01-01 4.032181 4.817497
2021-01-02 1.613846 1.882901
2021-01-03 1.910523 0.695274
2021-01-04 1.664737 4.107441
2021-01-05 2.454742 0.571687
2021-01-06 4.012355 1.680663
2021-01-07 4.228244 1.324920
2021-01-08 4.136020 0.272829
2021-01-09 1.895538 4.787343
2021-01-10 2.701062 2.634158
[3]:
weather_df.loc["2021-01-04":"2021-01-08"].style.pipe(make_pretty)
[3]:
Weather Conditions
  Tokyo Beijing
Monday Dry Heavy Rain
Tuesday Rain Dry
Wednesday Heavy Rain Dry
Thursday Heavy Rain Dry
Friday Heavy Rain Dry

Hiding Data#

The index and column headers can be completely hidden, as well subselecting rows or columns that one wishes to exclude. Both these options are performed using the same methods.

The index can be hidden from rendering by calling .hide() without any arguments, which might be useful if your index is integer based. Similarly column headers can be hidden by calling .hide(axis=”columns”) without any further arguments.

Specific rows or columns can be hidden from rendering by calling the same .hide() method and passing in a row/column label, a list-like or a slice of row/column labels to for the subset argument.

Hiding does not change the integer arrangement of CSS classes, e.g. hiding the first two columns of a DataFrame means the column class indexing will still start at col2, since col0 and col1 are simply ignored.

[4]:
df = pd.DataFrame(np.random.default_rng().standard_normal((5, 5)))
df.style.hide(subset=[0, 2, 4], axis=0).hide(subset=[0, 2, 4], axis=1)
[4]:
  1 3
1 0.302059 2.464724
3 -0.508091 1.542279

To invert the function to a show functionality it is best practice to compose a list of hidden items.

[5]:
show = [0, 2, 4]
df.style.hide([row for row in df.index if row not in show], axis=0).hide(
    [col for col in df.columns if col not in show], axis=1
)
[5]:
  0 2 4
0 -0.399220 0.885472 0.736064
2 -1.137847 0.195709 -0.411403
4 -1.565809 -1.527451 1.924920

Concatenating DataFrame Outputs#

Two or more Stylers can be concatenated together provided they share the same columns. This is very useful for showing summary statistics for a DataFrame, and is often used in combination with DataFrame.agg.

Since the objects concatenated are Stylers they can independently be styled as will be shown below and their concatenation preserves those styles.

[6]:
summary_styler = (
    df.agg(["sum", "mean"]).style.format(precision=3).relabel_index(["Sum", "Average"])
)
df.style.format(precision=1).concat(summary_styler)
[6]:
  0 1 2 3 4
0 -0.4 0.3 0.9 0.3 0.7
1 -0.0 0.3 0.9 2.5 1.2
2 -1.1 -0.6 0.2 0.4 -0.4
3 1.9 -0.5 1.3 1.5 -1.1
4 -1.6 -1.1 -1.5 1.8 1.9
Sum -1.195 -1.649 1.752 6.480 2.339
Average -0.239 -0.330 0.350 1.296 0.468

Styler Object and HTML#

The Styler was originally constructed to support the wide array of HTML formatting options. Its HTML output creates an HTML <table> and leverages CSS styling language to manipulate many parameters including colors, fonts, borders, background, etc. See here for more information on styling HTML tables. This allows a lot of flexibility out of the box, and even enables web developers to integrate DataFrames into their existing user interface designs.

Below we demonstrate the default output, which looks very similar to the standard DataFrame HTML representation. But the HTML here has already attached some CSS classes to each cell, even if we haven’t yet created any styles. We can view these by calling the .to_html() method, which returns the raw HTML as string, which is useful for further processing or adding to a file - read on in More about CSS and HTML. This section will also provide a walkthrough for how to convert this default output to represent a DataFrame output that is more communicative. For example how we can build s:

[7]:
idx = pd.Index(["Tumour (Positive)", "Non-Tumour (Negative)"], name="Actual Label:")
cols = pd.MultiIndex.from_product(
    [["Decision Tree", "Regression", "Random"], ["Tumour", "Non-Tumour"]],
    names=["Model:", "Predicted:"],
)
df = pd.DataFrame(
    [[38.0, 2.0, 18.0, 22.0, 21, np.nan], [19, 439, 6, 452, 226, 232]],
    index=idx,
    columns=cols,
)
df.style
[7]:
Model: Decision Tree Regression Random
Predicted: Tumour Non-Tumour Tumour Non-Tumour Tumour Non-Tumour
Actual Label:            
Tumour (Positive) 38.000000 2.000000 18.000000 22.000000 21 nan
Non-Tumour (Negative) 19.000000 439.000000 6.000000 452.000000 226 232.000000
[9]:
s
[9]:
Confusion matrix for multiple cancer prediction models.
Model: Decision Tree Regression
Predicted: Tumour Non-Tumour Tumour Non-Tumour
Actual Label:        
Tumour (Positive) 38 2 18 22
Non-Tumour (Negative) 19 439 6 452

The first step we have taken is the create the Styler object from the DataFrame and then select the range of interest by hiding unwanted columns with .hide().

[10]:
s = df.style.format("{:.0f}").hide(
    [("Random", "Tumour"), ("Random", "Non-Tumour")], axis="columns"
)
s
[10]:
Model: Decision Tree Regression
Predicted: Tumour Non-Tumour Tumour Non-Tumour
Actual Label:        
Tumour (Positive) 38 2 18 22
Non-Tumour (Negative) 19 439 6 452

Methods to Add Styles#

There are 3 primary methods of adding custom CSS styles to Styler:

  • Using .set_table_styles() to control broader areas of the table with specified internal CSS. Although table styles allow the flexibility to add CSS selectors and properties controlling all individual parts of the table, they are unwieldy for individual cell specifications. Also, note that table styles cannot be exported to Excel.

  • Using .set_td_classes() to directly link either external CSS classes to your data cells or link the internal CSS classes created by .set_table_styles(). See here. These cannot be used on column header rows or indexes, and also won’t export to Excel.

  • Using the .apply() and .map() functions to add direct internal CSS to specific data cells. See here. As of v1.4.0 there are also methods that work directly on column header rows or indexes: .apply_index() and .map_index(). Note that only these methods add styles that will export to Excel. These methods work in a similar way to DataFrame.apply() and DataFrame.map().

Table Styles#

Table styles are flexible enough to control all individual parts of the table, including column headers and indexes. However, they can be unwieldy to type for individual data cells or for any kind of conditional formatting, so we recommend that table styles are used for broad styling, such as entire rows or columns at a time.

Table styles are also used to control features which can apply to the whole table at once such as creating a generic hover functionality. The :hover pseudo-selector, as well as other pseudo-selectors, can only be used this way.

To replicate the normal format of CSS selectors and properties (attribute value pairs), e.g.

tr:hover {
  background-color: #ffff99;
}

the necessary format to pass styles to .set_table_styles() is as a list of dicts, each with a CSS-selector tag and CSS-properties. Properties can either be a list of 2-tuples, or a regular CSS-string, for example:

[12]:
cell_hover = {  # for row hover use <tr> instead of <td>
    "selector": "td:hover",
    "props": [("background-color", "#ffffb3")],
}
index_names = {
    "selector": ".index_name",
    "props": "font-style: italic; color: darkgrey; font-weight:normal;",
}
headers = {
    "selector": "th:not(.index_name)",
    "props": "background-color: #000066; color: white;",
}
s.set_table_styles([cell_hover, index_names, headers])
[12]:
Model: Decision Tree Regression
Predicted: Tumour Non-Tumour Tumour Non-Tumour
Actual Label:        
Tumour (Positive) 38 2 18 22
Non-Tumour (Negative) 19 439 6 452

Next we just add a couple more styling artifacts targeting specific parts of the table. Be careful here, since we are chaining methods we need to explicitly instruct the method not to overwrite the existing styles.

[14]:
s.set_table_styles(
    [
        {"selector": "th.col_heading", "props": "text-align: center;"},
        {"selector": "th.col_heading.level0", "props": "font-size: 1.5em;"},
        {"selector": "td", "props": "text-align: center; font-weight: bold;"},
    ],
    overwrite=False,
)
[14]:
Model: Decision Tree Regression
Predicted: Tumour Non-Tumour Tumour Non-Tumour
Actual Label:        
Tumour (Positive) 38 2 18 22
Non-Tumour (Negative) 19 439 6 452

As a convenience method (since version 1.2.0) we can also pass a dict to .set_table_styles() which contains row or column keys. Behind the scenes Styler just indexes the keys and adds relevant .col<m> or .row<n> classes as necessary to the given CSS selectors.

[16]:
s.set_table_styles(
    {
        ("Regression", "Tumour"): [
            {"selector": "th", "props": "border-left: 1px solid white"},
            {"selector": "td", "props": "border-left: 1px solid #000066"},
        ]
    },
    overwrite=False,
    axis=0,
)
[16]:
Model: Decision Tree Regression
Predicted: Tumour Non-Tumour Tumour Non-Tumour
Actual Label:        
Tumour (Positive) 38 2 18 22
Non-Tumour (Negative) 19 439 6 452

Setting Classes and Linking to External CSS#

If you have designed a website then it is likely you will already have an external CSS file that controls the styling of table and cell objects within it. You may want to use these native files rather than duplicate all the CSS in python (and duplicate any maintenance work).

Table Attributes#

It is very easy to add a class to the main <table> using .set_table_attributes(). This method can also attach inline styles - read more in CSS Hierarchies.

[18]:
out = s.set_table_attributes('class="my-table-cls"').to_html()
print(out[out.find("<table") :][:109])
<table id="T_xyz01" class="my-table-cls">
  <thead>
    <tr>
      <th class="index_name level0" >Model:</th>

Data Cell CSS Classes#

New in version 1.2.0

The .set_td_classes() method accepts a DataFrame with matching indices and columns to the underlying Styler’s DataFrame. That DataFrame will contain strings as css-classes to add to individual data cells: the <td> elements of the <table>. Rather than use external CSS we will create our classes internally and add them to table style. We will save adding the borders until the section on tooltips.

[19]:
s.set_table_styles(
    [  # create internal CSS classes
        {"selector": ".true", "props": "background-color: #e6ffe6;"},
        {"selector": ".false", "props": "background-color: #ffe6e6;"},
    ],
    overwrite=False,
)
cell_color = pd.DataFrame(
    [["true ", "false ", "true ", "false "], ["false ", "true ", "false ", "true "]],
    index=df.index,
    columns=df.columns[:4],
)
s.set_td_classes(cell_color)
[19]:
Model: Decision Tree Regression
Predicted: Tumour Non-Tumour Tumour Non-Tumour
Actual Label:        
Tumour (Positive) 38 2 18 22
Non-Tumour (Negative) 19 439 6 452

Styler Functions#

Acting on Data#

We use the following methods to pass your style functions. Both of those methods take a function (and some other keyword arguments) and apply it to the DataFrame in a certain way, rendering CSS styles.

  • .map() (elementwise): accepts a function that takes a single value and returns a string with the CSS attribute-value pair.

  • .apply() (column-/row-/table-wise): accepts a function that takes a Series or DataFrame and returns a Series, DataFrame, or numpy array with an identical shape where each element is a string with a CSS attribute-value pair. This method passes each column or row of 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.

This method is powerful for applying multiple, complex logic to data cells. We create a new DataFrame to demonstrate this.

[21]:
df2 = pd.DataFrame(
    np.random.default_rng(0).standard_normal((10, 4)), columns=["A", "B", "C", "D"]
)
df2.style
[21]:
  A B C D
0 0.125730 -0.132105 0.640423 0.104900
1 -0.535669 0.361595 1.304000 0.947081
2 -0.703735 -1.265421 -0.623274 0.041326
3 -2.325031 -0.218792 -1.245911 -0.732267
4 -0.544259 -0.316300 0.411631 1.042513
5 -0.128535 1.366463 -0.665195 0.351510
6 0.903470 0.094012 -0.743499 -0.921725
7 -0.457726 0.220195 -1.009618 -0.209176
8 -0.159225 0.540846 0.214659 0.355373
9 -0.653829 -0.129614 0.783975 1.493431

For example we can build a function that colors text if it is negative, and chain this with a function that partially fades cells of negligible value. Since this looks at each element in turn we use map.

[22]:
def style_negative(v, props=""):
    return props if v < 0 else None


s2 = df2.style.map(style_negative, props="color:red;").map(
    lambda v: "opacity: 20%;" if (v < 0.3) and (v > -0.3) else None
)
s2
[22]:
  A B C D
0 0.125730 -0.132105 0.640423 0.104900
1 -0.535669 0.361595 1.304000 0.947081
2 -0.703735 -1.265421 -0.623274 0.041326
3 -2.325031 -0.218792 -1.245911 -0.732267
4 -0.544259 -0.316300 0.411631 1.042513
5 -0.128535 1.366463 -0.665195 0.351510
6 0.903470 0.094012 -0.743499 -0.921725
7 -0.457726 0.220195 -1.009618 -0.209176
8 -0.159225 0.540846 0.214659 0.355373
9 -0.653829 -0.129614 0.783975 1.493431

We can also build a function that highlights the maximum value across rows, cols, and the DataFrame all at once. In this case we use apply. Below we highlight the maximum in a column.

[24]:
def highlight_max(s, props=""):
    return np.where(s == np.nanmax(s.values), props, "")


s2.apply(highlight_max, props="color:white;background-color:darkblue", axis=0)
[24]:
  A B C D
0 0.125730 -0.132105 0.640423 0.104900
1 -0.535669 0.361595 1.304000 0.947081
2 -0.703735 -1.265421 -0.623274 0.041326
3 -2.325031 -0.218792 -1.245911 -0.732267
4 -0.544259 -0.316300 0.411631 1.042513
5 -0.128535 1.366463 -0.665195 0.351510
6 0.903470 0.094012 -0.743499 -0.921725
7 -0.457726 0.220195 -1.009618 -0.209176
8 -0.159225 0.540846 0.214659 0.355373
9 -0.653829 -0.129614 0.783975 1.493431

We can use the same function across the different axes, highlighting here the DataFrame maximum in purple, and row maximums in pink.

[26]:
s2.apply(highlight_max, props="color:white;background-color:pink;", axis=1).apply(
    highlight_max, props="color:white;background-color:purple", axis=None
)
[26]:
  A B C D
0 0.125730 -0.132105 0.640423 0.104900
1 -0.535669 0.361595 1.304000 0.947081
2 -0.703735 -1.265421 -0.623274 0.041326
3 -2.325031 -0.218792 -1.245911 -0.732267
4 -0.544259 -0.316300 0.411631 1.042513
5 -0.128535 1.366463 -0.665195 0.351510
6 0.903470 0.094012 -0.743499 -0.921725
7 -0.457726 0.220195 -1.009618 -0.209176
8 -0.159225 0.540846 0.214659 0.355373
9 -0.653829 -0.129614 0.783975 1.493431

This last example shows how some styles have been overwritten by others. In general the most recent style applied is active but you can read more in the section on CSS hierarchies. You can also apply these styles to more granular parts of the DataFrame - read more in section on subset slicing.

It is possible to replicate some of this functionality using just classes but it can be more cumbersome. See item 3) of Optimization

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, and with DataFrame.apply you will be able to inspect the CSS string output of your intended function in each cell.

Acting on the Index and Column Headers#

Similar application is achieved for headers by using:

  • .map_index() (elementwise): accepts a function that takes a single value and returns a string with the CSS attribute-value pair.

  • .apply_index() (level-wise): accepts a function that takes a Series and returns a Series, or numpy array with an identical shape where each element is a string with a CSS attribute-value pair. This method passes each level of your Index one-at-a-time. To style the index use axis=0 and to style the column headers use axis=1.

You can select a level of a MultiIndex but currently no similar subset application is available for these methods.

[28]:
s2.map_index(lambda v: "color:pink;" if v > 4 else "color:darkblue;", axis=0)
s2.apply_index(
    lambda s: np.where(s.isin(["A", "B"]), "color:pink;", "color:darkblue;"), axis=1
)
[28]:
  A B C D
0 0.125730 -0.132105 0.640423 0.104900
1 -0.535669 0.361595 1.304000 0.947081
2 -0.703735 -1.265421 -0.623274 0.041326
3 -2.325031 -0.218792 -1.245911 -0.732267
4 -0.544259 -0.316300 0.411631 1.042513
5 -0.128535 1.366463 -0.665195 0.351510
6 0.903470 0.094012 -0.743499 -0.921725
7 -0.457726 0.220195 -1.009618 -0.209176
8 -0.159225 0.540846 0.214659 0.355373
9 -0.653829 -0.129614 0.783975 1.493431

Tooltips and Captions#

Table captions can be added with the .set_caption() method. You can use table styles to control the CSS relevant to the caption.

[29]:
s.set_caption(
    "Confusion matrix for multiple cancer prediction models."
).set_table_styles(
    [{"selector": "caption", "props": "caption-side: bottom; font-size:1.25em;"}],
    overwrite=False,
)
[29]:
Confusion matrix for multiple cancer prediction models.
Model: Decision Tree Regression
Predicted: Tumour Non-Tumour Tumour Non-Tumour
Actual Label:        
Tumour (Positive) 38 2 18 22
Non-Tumour (Negative) 19 439 6 452

Adding tooltips (since version 1.3.0) can be done using the .set_tooltips() method in the same way you can add CSS classes to data cells by providing a string based DataFrame with intersecting indices and columns. You don’t have to specify a css_class name or any css props for the tooltips, since there are standard defaults, but the option is there if you want more visual control.

[31]:
tt = pd.DataFrame(
    [
        [
            "This model has a very strong true positive rate",
            "This model's total number of false negatives is too high",
        ]
    ],
    index=["Tumour (Positive)"],
    columns=df.columns[[0, 3]],
)
s.set_tooltips(
    tt,
    props="visibility: hidden; position: absolute; z-index: 1; "
    "border: 1px solid #000066;"
    "background-color: white; color: #000066; font-size: 0.8em;"
    "transform: translate(0px, -24px); padding: 0.6em; "
    "border-radius: 0.5em;",
)
[31]:
Confusion matrix for multiple cancer prediction models.
Model: Decision Tree Regression
Predicted: Tumour Non-Tumour Tumour Non-Tumour
Actual Label:        
Tumour (Positive) 38 2 18 22
Non-Tumour (Negative) 19 439 6 452

The only thing left to do for our table is to add the highlighting borders to draw the audience attention to the tooltips. We will create internal CSS classes as before using table styles. Setting classes always overwrites so we need to make sure we add the previous classes.

[33]:
s.set_table_styles(
    [  # create internal CSS classes
        {"selector": ".border-red", "props": "border: 2px dashed red;"},
        {"selector": ".border-green", "props": "border: 2px dashed green;"},
    ],
    overwrite=False,
)
cell_border = pd.DataFrame(
    [["border-green ", " ", " ", "border-red "], [" ", " ", " ", " "]],
    index=df.index,
    columns=df.columns[:4],
)
s.set_td_classes(cell_color + cell_border)
[33]:
Confusion matrix for multiple cancer prediction models.
Model: Decision Tree Regression
Predicted: Tumour Non-Tumour Tumour Non-Tumour
Actual Label:        
Tumour (Positive) 38 2 18 22
Non-Tumour (Negative) 19 439 6 452

Finer Control with Slicing#

The examples we have shown so far for the Styler.apply and Styler.map functions have not demonstrated the use of the subset argument. This is a useful argument which permits a lot of flexibility: it allows you to apply styles to specific rows or columns, without having to code that logic into your style function.

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) is treated as multiple column labels

  • A tuple is treated as (row_indexer, column_indexer)

Consider using pd.IndexSlice to construct the tuple for the last one. We will create a MultiIndexed DataFrame to demonstrate the functionality.

[35]:
df3 = pd.DataFrame(
    np.random.default_rng().standard_normal((4, 4)),
    pd.MultiIndex.from_product([["A", "B"], ["r1", "r2"]]),
    columns=["c1", "c2", "c3", "c4"],
)
df3
[35]:
c1 c2 c3 c4
A r1 0.472962 -0.713451 0.547370 1.710349
r2 -0.858880 -0.417555 -0.593869 -0.320305
B r1 -0.171749 0.843842 0.013460 1.320816
r2 -0.837307 1.022460 1.024871 -0.348727

We will use subset to highlight the maximum in the third and fourth columns with red text. We will highlight the subset sliced region in yellow.

[36]:
slice_ = ["c3", "c4"]
df3.style.apply(
    highlight_max, props="color:red;", axis=0, subset=slice_
).set_properties(**{"background-color": "#ffffb3"}, subset=slice_)
[36]:
    c1 c2 c3 c4
A r1 0.472962 -0.713451 0.547370 1.710349
r2 -0.858880 -0.417555 -0.593869 -0.320305
B r1 -0.171749 0.843842 0.013460 1.320816
r2 -0.837307 1.022460 1.024871 -0.348727

If combined with the IndexSlice as suggested then it can index across both dimensions with greater flexibility.

[37]:
idx = pd.IndexSlice
slice_ = idx[idx[:, "r1"], idx["c2":"c4"]]
df3.style.apply(
    highlight_max, props="color:red;", axis=0, subset=slice_
).set_properties(**{"background-color": "#ffffb3"}, subset=slice_)
[37]:
    c1 c2 c3 c4
A r1 0.472962 -0.713451 0.547370 1.710349
r2 -0.858880 -0.417555 -0.593869 -0.320305
B r1 -0.171749 0.843842 0.013460 1.320816
r2 -0.837307 1.022460 1.024871 -0.348727

This also provides the flexibility to sub select rows when used with the axis=1.

[38]:
slice_ = idx[idx[:, "r2"], :]
df3.style.apply(
    highlight_max, props="color:red;", axis=1, subset=slice_
).set_properties(**{"background-color": "#ffffb3"}, subset=slice_)
[38]:
    c1 c2 c3 c4
A r1 0.472962 -0.713451 0.547370 1.710349
r2 -0.858880 -0.417555 -0.593869 -0.320305
B r1 -0.171749 0.843842 0.013460 1.320816
r2 -0.837307 1.022460 1.024871 -0.348727

There is also scope to provide conditional filtering.

Suppose we want to highlight the maximum across columns 2 and 4 only in the case that the sum of columns 1 and 3 is less than -2.0 (essentially excluding rows (:,'r2')).

[39]:
slice_ = idx[idx[(df3["c1"] + df3["c3"]) < -2.0], ["c2", "c4"]]
df3.style.apply(
    highlight_max, props="color:red;", axis=1, subset=slice_
).set_properties(**{"background-color": "#ffffb3"}, subset=slice_)
[39]:
    c1 c2 c3 c4
A r1 0.472962 -0.713451 0.547370 1.710349
r2 -0.858880 -0.417555 -0.593869 -0.320305
B r1 -0.171749 0.843842 0.013460 1.320816
r2 -0.837307 1.022460 1.024871 -0.348727

Only label-based slicing is supported right now, not positional, and not callables.

If your style function uses a subset or axis keyword argument, consider wrapping your function in a functools.partial, partialing out that keyword.

my_func2 = functools.partial(my_func, subset=42)

Optimization#

Generally, for smaller tables and most cases, the rendered HTML does not need to be optimized, and we don’t really recommend it. There are two cases where it is worth considering:

  • If you are rendering and styling a very large HTML table, certain browsers have performance issues.

  • If you are using Styler to dynamically create part of online user interfaces and want to improve network performance.

Here we recommend the following steps to implement:

1. Remove UUID and cell_ids#

Ignore the uuid and set cell_ids to False. This will prevent unnecessary HTML.

This is sub-optimal:

[40]:
df4 = pd.DataFrame([[1, 2], [3, 4]])
s4 = df4.style

This is better:

[41]:
from pandas.io.formats.style import Styler

s4 = Styler(df4, uuid_len=0, cell_ids=False)

2. Use table styles#

Use table styles where possible (e.g. for all cells or rows or columns at a time) since the CSS is nearly always more efficient than other formats.

This is sub-optimal:

[42]:
props = 'font-family: "Times New Roman", Times, serif; color: #e83e8c; font-size:1.3em;'
df4.style.map(lambda x: props, subset=[1])
[42]:
  0 1
0 1 2
1 3 4

This is better:

[43]:
df4.style.set_table_styles([{"selector": "td.col1", "props": props}])
[43]:
  0 1
0 1 2
1 3 4

3. Set classes instead of using Styler functions#

For large DataFrames where the same style is applied to many cells it can be more efficient to declare the styles as classes and then apply those classes to data cells, rather than directly applying styles to cells. It is, however, probably still easier to use the Styler function api when you are not concerned about optimization.

This is sub-optimal:

[44]:
df2.style.apply(
    highlight_max, props="color:white;background-color:darkblue;", axis=0
).apply(highlight_max, props="color:white;background-color:pink;", axis=1).apply(
    highlight_max, props="color:white;background-color:purple", axis=None
)
[44]:
  A B C D
0 0.125730 -0.132105 0.640423 0.104900
1 -0.535669 0.361595 1.304000 0.947081
2 -0.703735 -1.265421 -0.623274 0.041326
3 -2.325031 -0.218792 -1.245911 -0.732267
4 -0.544259 -0.316300 0.411631 1.042513
5 -0.128535 1.366463 -0.665195 0.351510
6 0.903470 0.094012 -0.743499 -0.921725
7 -0.457726 0.220195 -1.009618 -0.209176
8 -0.159225 0.540846 0.214659 0.355373
9 -0.653829 -0.129614 0.783975 1.493431

This is better:

[45]:
build = lambda x: pd.DataFrame(x, index=df2.index, columns=df2.columns)
cls1 = build(df2.apply(highlight_max, props="cls-1 ", axis=0))
cls2 = build(
    df2.apply(highlight_max, props="cls-2 ", axis=1, result_type="expand").values
)
cls3 = build(highlight_max(df2, props="cls-3 "))
df2.style.set_table_styles(
    [
        {"selector": ".cls-1", "props": "color:white;background-color:darkblue;"},
        {"selector": ".cls-2", "props": "color:white;background-color:pink;"},
        {"selector": ".cls-3", "props": "color:white;background-color:purple;"},
    ]
).set_td_classes(cls1 + cls2 + cls3)
[45]:
  A B C D
0 0.125730 -0.132105 0.640423 0.104900
1 -0.535669 0.361595 1.304000 0.947081
2 -0.703735 -1.265421 -0.623274 0.041326
3 -2.325031 -0.218792 -1.245911 -0.732267
4 -0.544259 -0.316300 0.411631 1.042513
5 -0.128535 1.366463 -0.665195 0.351510
6 0.903470 0.094012 -0.743499 -0.921725
7 -0.457726 0.220195 -1.009618 -0.209176
8 -0.159225 0.540846 0.214659 0.355373
9 -0.653829 -0.129614 0.783975 1.493431

4. Don’t use tooltips#

Tooltips require cell_ids to work and they generate extra HTML elements for every data cell.

5. If every byte counts use string replacement#

You can remove unnecessary HTML, or shorten the default class names by replacing the default css dict. You can read a little more about CSS below.

[46]:
my_css = {
    "row_heading": "",
    "col_heading": "",
    "index_name": "",
    "col": "c",
    "row": "r",
    "col_trim": "",
    "row_trim": "",
    "level": "l",
    "data": "",
    "blank": "",
}
html = Styler(df4, uuid_len=0, cell_ids=False)
html.set_table_styles(
    [
        {"selector": "td", "props": props},
        {"selector": ".c1", "props": "color:green;"},
        {"selector": ".l0", "props": "color:blue;"},
    ],
    css_class_names=my_css,
)
print(html.to_html())
<style type="text/css">
#T_ td {
  font-family: "Times New Roman", Times, serif;
  color: #e83e8c;
  font-size: 1.3em;
}
#T_ .c1 {
  color: green;
}
#T_ .l0 {
  color: blue;
}
</style>
<table id="T_">
  <thead>
    <tr>
      <th class=" l0" >&nbsp;</th>
      <th class=" l0 c0" >0</th>
      <th class=" l0 c1" >1</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th class=" l0 r0" >0</th>
      <td class=" r0 c0" >1</td>
      <td class=" r0 c1" >2</td>
    </tr>
    <tr>
      <th class=" l0 r1" >1</th>
      <td class=" r1 c0" >3</td>
      <td class=" r1 c1" >4</td>
    </tr>
  </tbody>
</table>

[47]:
html
[47]:
  0 1
0 1 2
1 3 4

Builtin Styles#

Some styling functions are common enough that we’ve “built them in” to the Styler, so you don’t have to write them and apply them yourself. The current list of such functions is:

The individual documentation on each function often gives more examples of their arguments.

Highlight Null#

[48]:
df2.iloc[0, 2] = np.nan
df2.iloc[4, 3] = np.nan
df2.loc[:4].style.highlight_null(color="yellow")
[48]:
  A B C D
0 0.125730 -0.132105 nan 0.104900
1 -0.535669 0.361595 1.304000 0.947081
2 -0.703735 -1.265421 -0.623274 0.041326
3 -2.325031 -0.218792 -1.245911 -0.732267
4 -0.544259 -0.316300 0.411631 nan

Highlight Min or Max#

[49]:
df2.loc[:4].style.highlight_max(
    axis=1, props=("color:white; " "font-weight:bold; " "background-color:darkblue;")
)
[49]:
  A B C D
0 0.125730 -0.132105 nan 0.104900
1 -0.535669 0.361595 1.304000 0.947081
2 -0.703735 -1.265421 -0.623274 0.041326
3 -2.325031 -0.218792 -1.245911 -0.732267
4 -0.544259 -0.316300 0.411631 nan

Highlight Between#

This method accepts ranges as float, or NumPy arrays or Series provided the indexes match.

[50]:
left = pd.Series([1.0, 0.0, 1.0], index=["A", "B", "D"])
df2.loc[:4].style.highlight_between(
    left=left, right=1.5, axis=1, props="color:white; background-color:purple;"
)
[50]:
  A B C D
0 0.125730 -0.132105 nan 0.104900
1 -0.535669 0.361595 1.304000 0.947081
2 -0.703735 -1.265421 -0.623274 0.041326
3 -2.325031 -0.218792 -1.245911 -0.732267
4 -0.544259 -0.316300 0.411631 nan

Highlight Quantile#

Useful for detecting the highest or lowest percentile values

[51]:
df2.loc[:4].style.highlight_quantile(q_left=0.85, axis=None, color="yellow")
[51]:
  A B C D
0 0.125730 -0.132105 nan 0.104900
1 -0.535669 0.361595 1.304000 0.947081
2 -0.703735 -1.265421 -0.623274 0.041326
3 -2.325031 -0.218792 -1.245911 -0.732267
4 -0.544259 -0.316300 0.411631 nan

Background Gradient and Text Gradient#

You can create “heatmaps” with the background_gradient and text_gradient methods. These require matplotlib, and we’ll use Seaborn to get a nice colormap.

[52]:
import seaborn as sns

cm = sns.light_palette("green", as_cmap=True)

df2.style.background_gradient(cmap=cm)
[52]:
  A B C D
0 0.125730 -0.132105 nan 0.104900
1 -0.535669 0.361595 1.304000 0.947081
2 -0.703735 -1.265421 -0.623274 0.041326
3 -2.325031 -0.218792 -1.245911 -0.732267
4 -0.544259 -0.316300 0.411631 nan
5 -0.128535 1.366463 -0.665195 0.351510
6 0.903470 0.094012 -0.743499 -0.921725
7 -0.457726 0.220195 -1.009618 -0.209176
8 -0.159225 0.540846 0.214659 0.355373
9 -0.653829 -0.129614 0.783975 1.493431
[53]:
df2.style.text_gradient(cmap=cm)
[53]:
  A B C D
0 0.125730 -0.132105 nan 0.104900
1 -0.535669 0.361595 1.304000 0.947081
2 -0.703735 -1.265421 -0.623274 0.041326
3 -2.325031 -0.218792 -1.245911 -0.732267
4 -0.544259 -0.316300 0.411631 nan
5 -0.128535 1.366463 -0.665195 0.351510
6 0.903470 0.094012 -0.743499 -0.921725
7 -0.457726 0.220195 -1.009618 -0.209176
8 -0.159225 0.540846 0.214659 0.355373
9 -0.653829 -0.129614 0.783975 1.493431

.background_gradient and .text_gradient have a number of keyword arguments to customise the gradients and colors. See the documentation.

Set properties#

Use Styler.set_properties when the style doesn’t actually depend on the values. This is just a simple wrapper for .map where the function returns the same properties for all cells.

[54]:
df2.loc[:4].style.set_properties(
    **{"background-color": "black", "color": "lawngreen", "border-color": "white"}
)
[54]:
  A B C D
0 0.125730 -0.132105 nan 0.104900
1 -0.535669 0.361595 1.304000 0.947081
2 -0.703735 -1.265421 -0.623274 0.041326
3 -2.325031 -0.218792 -1.245911 -0.732267
4 -0.544259 -0.316300 0.411631 nan

Bar charts#

You can include “bar charts” in your DataFrame.

[55]:
df2.style.bar(subset=["A", "B"], color="#d65f5f")
[55]:
  A B C D
0 0.125730 -0.132105 nan 0.104900
1 -0.535669 0.361595 1.304000 0.947081
2 -0.703735 -1.265421 -0.623274 0.041326
3 -2.325031 -0.218792 -1.245911 -0.732267
4 -0.544259 -0.316300 0.411631 nan
5 -0.128535 1.366463 -0.665195 0.351510
6 0.903470 0.094012 -0.743499 -0.921725
7 -0.457726 0.220195 -1.009618 -0.209176
8 -0.159225 0.540846 0.214659 0.355373
9 -0.653829 -0.129614 0.783975 1.493431

Additional keyword arguments give more control on centering and positioning, and you can pass a list of [color_negative, color_positive] to highlight lower and higher values or a matplotlib colormap.

To showcase an example here’s how you can change the above with the new align option, combined with setting vmin and vmax limits, the width of the figure, and underlying css props of cells, leaving space to display the text and the bars. We also use text_gradient to color the text the same as the bars using a matplotlib colormap (although in this case the visualization is probably better without this additional effect).

[56]:
df2.style.format("{:.3f}", na_rep="").bar(
    align=0,
    vmin=-2.5,
    vmax=2.5,
    cmap="bwr",
    height=50,
    width=60,
    props="width: 120px; border-right: 1px solid black;",
).text_gradient(cmap="bwr", vmin=-2.5, vmax=2.5)
[56]:
  A B C D
0 0.126 -0.132 0.105
1 -0.536 0.362 1.304 0.947
2 -0.704 -1.265 -0.623 0.041
3 -2.325 -0.219 -1.246 -0.732
4 -0.544 -0.316 0.412
5 -0.129 1.366 -0.665 0.352
6 0.903 0.094 -0.743 -0.922
7 -0.458 0.220 -1.010 -0.209
8 -0.159 0.541 0.215 0.355
9 -0.654 -0.130 0.784 1.493

The following example aims to give a highlight of the behavior of the new align options:

[58]:
HTML(head)
[58]:
Align All Negative Both Neg and Pos All Positive Large Positive
left
-100
-60
-30
-20
-10
-5
0
90
10
20
50
100
100
103
101
102
right
-100
-60
-30
-20
-10
-5
0
90
10
20
50
100
100
103
101
102
zero
-100
-60
-30
-20
-10
-5
0
90
10
20
50
100
100
103
101
102
mid
-100
-60
-30
-20
-10
-5
0
90
10
20
50
100
100
103
101
102
mean
-100
-60
-30
-20
-10
-5
0
90
10
20
50
100
100
103
101
102
99
-100
-60
-30
-20
-10
-5
0
90
10
20
50
100
100
103
101
102

Sharing styles#

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

[59]:
style1 = (
    df2.style.map(style_negative, props="color:red;")
    .map(lambda v: "opacity: 20%;" if (v < 0.3) and (v > -0.3) else None)
    .set_table_styles([{"selector": "th", "props": "color: blue;"}])
    .hide(axis="index")
)
style1
[59]:
A B C D
0.125730 -0.132105 nan 0.104900
-0.535669 0.361595 1.304000 0.947081
-0.703735 -1.265421 -0.623274 0.041326
-2.325031 -0.218792 -1.245911 -0.732267
-0.544259 -0.316300 0.411631 nan
-0.128535 1.366463 -0.665195 0.351510
0.903470 0.094012 -0.743499 -0.921725
-0.457726 0.220195 -1.009618 -0.209176
-0.159225 0.540846 0.214659 0.355373
-0.653829 -0.129614 0.783975 1.493431
[60]:
style2 = df3.style
style2.use(style1.export())
style2
[60]:
c1 c2 c3 c4
0.472962 -0.713451 0.547370 1.710349
-0.858880 -0.417555 -0.593869 -0.320305
-0.171749 0.843842 0.013460 1.320816
-0.837307 1.022460 1.024871 -0.348727

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.

Limitations#

  • DataFrame only (use Series.to_frame().style)

  • The index and columns do not need to be unique, but certain styling functions can only work with unique indexes.

  • No large repr, and construction performance isn’t great; although we have some HTML optimizations

  • You can only apply styles, you can’t insert new HTML entities, except via subclassing.

Other Fun and Useful Stuff#

Here are a few interesting examples.

Widgets#

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.

[61]:
from ipywidgets import widgets


@widgets.interact
def f(h_neg=(0, 359, 1), h_pos=(0, 359), s=(0.0, 99.9), l_post=(0.0, 99.9)):
    return df2.style.background_gradient(
        cmap=sns.palettes.diverging_palette(
            h_neg=h_neg, h_pos=h_pos, s=s, l=l_post, as_cmap=True
        )
    )

Magnify#

[62]:
def magnify():
    return [
        {"selector": "th", "props": [("font-size", "4pt")]},
        {"selector": "td", "props": [("padding", "0em 0em")]},
        {"selector": "th:hover", "props": [("font-size", "12pt")]},
        {
            "selector": "tr:hover td:hover",
            "props": [("max-width", "200px"), ("font-size", "12pt")],
        },
    ]
[63]:
cmap = sns.diverging_palette(5, 250, as_cmap=True)
bigdf = pd.DataFrame(np.random.default_rng(25).standard_normal((20, 25))).cumsum()

bigdf.style.background_gradient(cmap, axis=1).set_properties(
    **{"max-width": "80px", "font-size": "1pt"}
).set_caption("Hover to magnify").format(precision=2).set_table_styles(magnify())
[63]:
Hover to magnify
  0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
0 0.35 -0.00 -0.53 -2.28 0.02 0.93 1.04 -0.54 2.23 1.94 -0.23 -0.16 0.96 -0.25 0.22 1.22 1.19 -0.53 -0.46 -0.55 -0.01 0.07 -0.39 0.77 0.05
1 0.37 -1.97 -1.92 -2.97 0.38 0.06 2.60 -0.39 2.49 2.06 0.26 0.77 0.41 1.31 -0.55 1.74 0.98 -0.70 0.21 -0.35 1.59 -0.43 0.13 1.36 2.16
2 1.98 -1.27 -0.24 -2.12 -0.40 0.39 3.83 -0.91 4.01 2.42 -0.36 1.23 -1.16 0.62 -1.60 2.24 -0.04 -2.26 0.52 -0.83 0.87 -1.03 1.17 1.53 1.41
3 4.78 -0.78 1.67 -1.07 0.96 -0.48 3.26 -0.27 3.93 2.50 0.72 -1.07 -1.49 2.16 -2.07 2.28 0.34 -1.94 0.83 -1.76 1.20 -1.63 2.75 0.90 1.67
4 4.58 0.35 1.79 -0.36 0.40 -0.86 2.31 -0.44 3.65 1.36 -0.53 -0.91 -1.67 1.45 -2.99 1.17 0.44 -2.48 0.20 -2.32 0.73 -1.94 2.84 -0.86 1.41
5 3.38 -0.02 3.04 -2.28 -0.89 -1.38 0.01 1.95 3.01 2.98 -0.62 0.27 -0.86 1.15 -2.49 0.51 -0.04 -0.99 -0.67 -3.24 3.28 -2.33 3.14 0.78 0.92
6 2.81 1.80 3.02 -2.01 -1.23 -0.85 -1.57 2.12 4.01 3.94 -1.13 0.91 1.28 1.27 -1.81 0.15 -1.77 -0.25 -1.63 -3.94 3.34 -2.35 3.01 1.90 1.05
7 2.85 0.72 3.92 -3.29 -3.49 -1.67 -0.87 2.46 5.28 4.34 -0.76 1.24 0.64 0.81 -2.27 0.34 -2.71 -0.67 -1.35 -4.01 3.06 -2.55 1.78 0.54 0.25
8 3.80 1.51 4.19 -2.67 -2.71 -4.04 -1.42 3.30 7.04 4.52 0.96 2.40 0.78 -0.24 -3.28 0.28 -2.51 -0.74 -0.74 -3.44 3.11 -2.12 2.07 -1.35 -1.03
9 4.11 2.27 4.29 -2.97 -2.50 -3.58 -1.25 1.32 7.36 5.25 1.08 4.60 -0.64 -2.05 0.09 -0.72 -4.96 -1.31 0.09 -4.16 2.68 -1.37 4.02 -0.59 -0.72
10 3.67 3.02 7.08 -4.10 -3.57 -3.95 -0.53 1.94 7.14 4.78 2.44 5.06 0.69 -2.28 1.23 -1.56 -2.90 -1.71 -0.27 -3.72 2.50 -1.97 4.47 -1.23 1.21
11 2.98 3.85 6.33 -5.84 -5.90 -3.91 -1.94 2.08 6.33 4.70 2.41 6.84 0.50 -3.68 -1.10 -0.67 -2.80 -0.28 -1.88 -3.91 1.67 -1.48 4.15 0.17 0.79
12 2.76 3.21 6.80 -4.75 -5.36 -4.53 -3.00 1.17 6.52 4.20 2.25 7.97 0.98 -3.45 -1.59 -0.86 -3.09 -0.24 -2.08 -3.73 1.03 -0.18 4.54 0.48 1.69
13 1.69 2.02 5.31 -5.51 -4.79 -4.34 -1.42 -0.24 6.96 2.92 0.97 6.70 2.18 -3.38 -0.33 -0.77 -2.39 0.42 -1.53 -3.59 1.98 0.01 4.61 0.52 1.11
14 0.17 1.26 7.10 -5.35 -5.81 -4.61 -0.45 1.61 5.10 1.31 -1.18 6.09 1.25 -2.23 0.32 -1.33 -0.08 -0.19 -1.21 -2.83 0.97 0.13 4.89 0.36 1.83
15 0.01 0.61 7.94 -5.85 -5.35 -5.10 0.14 1.76 3.89 1.72 -1.61 7.86 0.71 -1.38 -1.95 -1.84 -1.88 -0.14 -2.02 -1.45 1.68 1.62 3.97 -1.88 1.80
16 1.42 0.91 9.86 -6.10 -6.02 -4.56 1.24 1.38 3.25 1.05 0.38 5.77 1.92 -1.51 -0.36 -1.01 -3.01 0.10 -2.50 -1.99 0.29 2.91 2.07 -1.64 3.87
17 1.94 2.55 10.78 -4.77 -6.87 -4.38 1.74 1.63 2.34 0.06 0.41 6.21 2.20 -3.13 -0.88 -1.57 -2.59 -0.76 -2.38 -3.21 -0.11 3.62 2.91 -2.87 3.98
18 1.27 4.45 11.25 -4.68 -4.93 -7.30 0.92 2.30 2.34 0.52 -0.15 6.01 2.52 -1.89 1.37 -2.65 -1.98 -2.85 -2.78 -2.81 -0.86 3.93 2.83 -1.04 4.17
19 0.72 2.89 10.94 -3.85 -2.50 -7.04 0.77 2.27 2.83 1.63 0.94 6.26 3.07 -1.99 2.65 -4.04 -0.91 -2.69 -2.70 -2.49 -0.86 3.16 2.88 -0.85 4.54

Sticky Headers#

If you display a large matrix or DataFrame in a notebook, but you want to always see the column and row headers you can use the .set_sticky method which manipulates the table styles CSS.

[64]:
bigdf = pd.DataFrame(np.random.default_rng().standard_normal((16, 100)))
bigdf.style.set_sticky(axis="index")
[64]:
  0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
0 -0.477904 0.255455 -1.036692 1.133506 -1.503100 0.444102 1.401428 0.290586 -1.783543 1.688557 -0.056869 -1.401320 -2.079290 0.336990 -0.707665 -0.005710 -0.105628 -0.576448 0.033234 1.110586 -0.122865 1.143149 -0.957502 0.921099 0.313698 1.215567 1.332542 -2.057070 1.141553 -0.294125 -1.305428 -0.268712 -0.852149 0.686259 1.407350 0.778940 0.836620 -0.771322 -2.071893 1.247859 0.140568 2.001109 0.840592 0.410528 -0.191591 -0.976692 -0.286939 1.302801 -1.007614 -0.318765 -0.088227 -0.345615 -0.830125 0.731525 0.904993 -0.804781 -0.334342 0.591921 -0.892399 0.173999 -0.987925 0.246079 -1.171178 2.150182 -0.620033 -0.554768 -0.352419 0.409642 0.717860 -0.842420 -0.174826 -2.539582 0.323395 -1.806040 -1.531367 -0.327377 0.866184 0.020804 -0.840086 -0.170454 -0.614758 -1.535173 -1.488820 -1.655628 -0.440489 0.616466 -0.252391 -0.424483 0.334900 -1.231898 -2.024635 -0.840675 0.978826 -1.002244 -0.582052 -0.486294 -0.506214 0.195567 -0.210379 0.738297
1 0.585237 2.647457 0.119200 -0.108335 0.668777 -1.007897 -0.536105 -0.125968 0.494865 -1.690365 0.429727 0.199385 -0.645263 0.017235 -0.645622 -0.673141 0.212909 -0.183091 -0.180927 1.755693 -0.338625 1.355682 0.256700 -0.188953 1.091026 -1.301383 0.973041 0.910566 -0.141481 -1.294820 -0.071232 1.653252 -2.671628 -0.223089 0.987800 0.594616 0.271735 -0.441871 1.141844 -0.104390 -0.782294 -2.443544 1.266727 -0.120827 -0.032833 0.053360 1.156297 0.692768 0.147312 0.949327 0.759757 1.879542 -0.131123 -0.873306 -0.157111 -0.253708 0.593872 -1.079665 1.324434 -1.840771 -0.017790 2.143841 -0.733744 -1.463399 0.103749 0.577465 -0.372991 1.432642 0.698148 0.464842 0.746067 1.341692 -0.030676 1.791347 -0.313512 -0.772671 -1.574769 0.733113 0.667523 -1.295449 -0.329993 -0.098159 1.397084 -0.614709 -2.142259 -0.678900 2.274475 0.082669 0.001121 -0.301682 -1.126572 -1.586037 0.211824 0.250514 0.686721 -0.620784 -0.727293 0.815589 -0.130651 -0.334826
2 -2.275237 -0.161309 -0.090888 -1.069049 1.103881 0.580332 -0.242143 -0.297919 -1.548967 0.424682 0.347525 -1.299632 2.057055 -0.033416 -1.032662 2.532423 -0.139160 -0.381236 0.002564 -0.355384 -0.387259 -0.029480 0.155327 0.019139 0.470035 -0.228955 0.961646 0.455171 2.298854 0.015216 -0.006006 0.942286 -0.075252 0.427614 -0.062118 -0.249663 -0.222687 0.755397 -0.489316 -0.339373 -2.220204 0.450844 1.168297 -1.021234 -0.653084 -0.512592 -0.029187 -0.776034 0.493463 1.018998 1.445182 0.991347 -1.361966 0.676086 -1.814587 -0.610042 -0.300384 0.925241 -0.799652 -0.993661 1.268798 0.848636 -0.265245 -0.186983 -1.820013 -0.466652 0.576109 0.439632 0.393298 -0.221681 -0.486075 0.621923 0.535757 -0.939688 1.043671 0.682593 -0.033550 1.140860 0.902243 -0.802460 -0.081606 -0.801892 1.351038 -0.494487 -0.520726 -0.315359 -0.312007 -0.669371 0.053571 -0.533757 -1.650414 -0.753615 0.398238 0.535282 2.289758 0.269020 -1.048343 0.978725 0.285308 0.221338
3 -0.153361 -0.544615 0.313869 0.571500 0.387451 -1.047070 2.342909 0.258259 -1.251172 0.661437 -1.142217 0.694600 -0.003757 1.737524 -0.754015 -0.088262 1.591698 -0.936549 1.991625 1.460563 0.805654 0.590560 -0.987085 0.971833 0.559503 -0.335600 -0.302770 1.525905 0.393735 -0.257535 0.551972 -0.000964 1.225352 -0.195811 -0.220533 2.850902 -0.540946 -0.141136 0.136409 -1.820779 -0.938299 1.504934 -0.460442 0.511671 0.590275 0.688136 -1.565189 1.332829 0.517363 -0.994249 -0.754060 -0.974907 1.448075 1.194312 0.732741 0.303084 0.424536 -0.696156 -0.190177 -0.706482 1.106542 0.913718 0.065983 0.443318 -1.662877 -0.935094 -1.770420 1.340994 0.037316 -0.158977 0.380143 -0.834214 -0.918589 0.392834 0.919642 -1.374318 0.517009 -0.144410 -0.139062 0.522213 -1.183980 0.783660 1.916785 -1.348197 -1.899979 0.821277 0.127906 1.091722 0.786539 0.679616 2.814977 -0.933216 -1.013307 0.896509 2.743150 0.500607 -0.771423 1.017250 -0.244201 0.777267
4 -0.587462 -0.789396 2.061587 -1.159394 1.412129 -0.708493 -2.339050 -1.320658 0.138352 0.547877 1.727875 -0.600047 2.348426 -0.971963 0.997073 1.563061 -0.602184 -0.203174 0.152164 0.456641 -0.229275 2.344958 0.716073 0.814685 -0.614382 1.122271 -0.095403 -0.627311 -0.557301 2.288314 0.875762 -0.063959 -0.226624 -3.506094 0.218051 -0.486305 -0.419128 -0.317432 1.430059 1.450187 -0.320155 1.246194 -0.870016 -0.115043 -0.264790 -1.187109 -0.459220 -1.531756 -2.165223 -0.111649 -0.473090 2.574181 -1.233349 0.450126 1.465904 -0.899823 0.327665 -0.863885 -2.225079 0.101733 -0.547152 -0.998309 -1.173454 -0.215375 -1.665647 0.391019 0.344336 1.741681 -0.863386 -1.304589 0.381078 -0.641358 0.259613 2.602810 0.430121 1.750116 -0.657066 1.376877 0.302335 -0.595907 -0.034496 0.932377 -0.113500 1.068749 -0.149410 0.210923 -0.677550 -0.077921 -0.048069 0.172445 -0.648020 0.734457 0.088277 0.485921 -0.581941 -0.470936 -0.056922 0.534501 -1.352339 -1.175363
5 0.873152 1.601969 0.178049 -1.983827 -0.622169 -0.664296 -1.242833 -1.689207 0.495401 1.720645 -1.126703 -0.405589 0.736988 -1.023278 -1.020098 0.116688 0.866702 1.583894 -0.110518 -1.959511 0.127211 0.309706 0.138433 1.090373 -0.248531 0.838397 -0.213734 -1.341715 0.938799 0.294882 0.068500 -0.878982 0.822640 -0.351942 -0.147947 -0.391392 -1.966654 0.219815 -2.007916 -1.705208 -0.567268 -0.177842 1.851972 1.315402 0.069730 -0.335549 0.572100 0.743115 0.252673 0.779700 -0.003330 -0.262999 0.883145 1.155826 1.220540 0.859195 -0.415208 -1.994984 0.039692 -0.992914 -1.654410 -0.538202 -0.145346 0.009721 0.726617 0.582571 -0.643513 -1.250474 -0.904109 0.695077 -2.134919 -0.755469 -0.744455 0.762004 -0.496951 0.235681 0.621123 0.624283 -0.365994 -0.321027 0.610761 0.731269 -0.096978 1.557334 0.010992 0.954745 -2.251012 -0.066306 0.812171 0.621621 -0.309016 0.517246 -2.180666 -0.832025 0.924467 -1.426500 0.984203 -0.474932 0.175826 0.335576
6 0.433552 -0.201430 -0.943410 -1.042909 0.034170 -1.541850 -1.195631 0.887972 0.393069 -0.958733 -0.072806 0.319061 0.391631 -0.031760 1.371569 -0.040696 0.843700 1.483889 -1.211425 -0.699957 1.422021 0.717107 -0.724958 0.771420 -0.440838 0.761756 2.333486 -1.561506 -0.420946 -0.734936 1.136618 1.606310 0.049381 1.444088 -0.788869 -0.462994 -1.048723 -0.551249 -0.335222 0.402854 -0.804416 -0.638295 0.415961 -0.137121 0.185281 -0.696716 0.113364 1.274840 -0.391330 0.578917 0.430885 0.611646 1.124741 0.868773 0.304308 0.312547 0.315105 -0.917382 -0.187145 0.310147 1.232167 1.489123 0.529581 0.701434 -1.296493 -0.504178 -0.704184 1.080987 -0.124031 0.811502 0.373988 -0.828847 0.423870 0.292172 0.294221 -1.865392 -0.843158 -0.601878 -0.558144 -0.103680 0.126211 -0.448466 0.033401 1.513842 0.027381 -1.746780 0.695925 0.330964 -0.490750 -1.019039 -0.810897 -0.777655 1.109129 -1.257298 0.837863 -1.551229 0.057772 0.002614 1.915058 -0.269061
7 0.474834 0.957474 -0.388375 0.689624 1.383813 -1.470173 -0.149235 -2.435437 1.347520 -0.563205 1.510469 0.594204 -1.060062 1.009238 2.086382 0.764776 0.226951 -1.412304 0.723599 -0.525493 -0.694461 0.459322 -0.633279 -1.406308 1.070413 -1.172406 0.796120 -0.617709 -0.609522 -1.405683 -0.335592 -0.715788 0.745924 -0.578628 0.989271 0.891024 -0.524736 0.150400 -0.280605 -0.309595 -0.090330 0.921847 1.898539 1.930585 -1.439397 -0.054393 -0.319578 -0.081519 -0.217571 -0.129922 -0.977441 0.606411 0.770752 -1.015290 0.709502 -1.402151 0.967668 -0.331202 -1.100451 0.157843 0.146878 0.616650 -1.309539 -0.015751 0.562530 0.332627 -0.613157 -0.923942 0.496891 1.420402 -0.501321 0.902524 -0.049179 1.183360 2.212491 -0.671855 -0.751182 0.246615 0.474175 1.217908 0.281949 0.205916 -1.236634 2.202909 -0.332121 -0.461662 1.414995 0.381989 -0.269530 0.697302 -0.787901 0.816281 -0.489125 0.657600 -1.365946 0.784740 -0.080619 -0.978908 -0.312965 0.890678
8 0.394178 -0.399837 -0.515396 0.034383 -0.556831 0.401657 -0.678602 -0.199930 -1.609916 0.518976 -1.010074 2.760926 0.404390 -0.799003 -1.868415 2.071176 -0.513190 -0.202935 1.178557 0.157075 -0.250039 1.048015 -0.046891 0.164406 0.508845 -0.126983 1.612909 -0.747256 -1.414439 -0.528527 0.897602 1.274353 -0.982594 0.015758 -0.391586 -1.179800 -1.354369 1.525614 -1.183777 -0.286996 0.421969 -1.455870 -1.225708 0.399306 -1.455237 -0.219310 -0.655527 0.588820 0.894316 0.149485 0.297184 0.494854 -0.225150 -1.232678 -0.395888 1.617737 0.419797 -1.117645 -0.060077 0.775085 0.231908 -1.118163 -0.742908 -0.406036 0.054308 0.418820 -0.777331 -0.536132 -0.420887 0.707553 0.158623 0.717902 0.988025 -0.557313 2.271761 0.462317 1.049660 -0.014489 0.667679 -1.087098 -0.006715 0.117394 -1.049528 0.263727 -0.119058 0.460560 1.376910 -0.754072 0.622208 -0.734321 -1.506515 1.774620 -0.311566 -1.473624 -0.747015 0.319220 -1.373398 -2.811869 -0.613424 0.621426
9 1.671636 -0.038020 1.222513 -0.058518 0.933328 0.800803 0.692282 0.829730 -1.122427 -0.936183 -1.435730 -1.666693 1.022173 2.285226 -1.444257 -1.495789 1.262512 -1.605591 -0.505053 -0.963444 -0.226690 -0.171874 -0.031180 -0.353005 -2.778255 -0.234732 -0.438278 0.720178 1.758388 0.491887 1.081333 0.074762 -1.028180 1.304382 1.312343 -0.087118 -2.581515 -0.077336 1.250050 -0.938518 -0.246761 0.473507 -0.116527 -0.759301 -0.998732 0.738178 0.661729 -0.139460 -0.239646 1.227980 -1.136144 0.262543 -1.418194 -1.282242 0.188574 -0.244128 0.037893 0.531271 0.734701 -0.792197 -0.194756 -0.426177 2.030788 2.307577 -0.755726 2.037830 -1.158194 0.104399 -0.629492 0.489047 0.071801 -2.074276 0.729743 -0.102213 0.011607 0.478700 1.154224 -0.684764 -0.873370 -0.062123 0.099357 1.350460 -0.044939 0.313854 -0.076810 -1.330002 -0.130381 0.356302 0.897122 0.436875 -1.715499 -0.218792 0.396300 -0.099376 1.134847 1.295450 0.434639 1.024399 -1.641434 -0.736609
10 1.082696 0.031717 2.209015 -0.013215 -2.003879 -0.161916 -0.661974 0.560688 -0.930355 -0.510425 -0.578507 -1.647700 0.755420 -0.518368 0.170730 -2.200226 -0.823591 0.465074 -0.464247 -0.099755 0.465379 0.378180 3.238894 -0.776928 -0.881090 -0.453011 0.862192 -0.144060 0.097043 -0.097493 -2.192043 0.138721 -0.865865 -0.791182 0.438490 -0.033203 -1.385018 0.225164 -1.097561 -0.437453 0.197283 -0.899327 0.350646 -0.170051 -0.379607 -0.960015 0.039552 -0.105490 0.807139 -0.599240 -0.069271 -2.185495 0.021436 -0.241760 0.315148 0.833108 -0.557417 -0.482816 0.638546 0.032573 0.334365 0.798180 -0.608151 0.809502 2.421493 0.505470 -2.006477 0.755333 1.045566 0.182879 0.289520 0.729436 0.796884 -1.915597 0.105973 -0.284472 -0.723593 1.055573 0.297566 1.404749 -1.191471 -0.872898 1.131912 0.649634 -0.748611 -0.368370 -1.791016 0.631735 -0.095272 1.651556 -0.108445 0.134226 -0.493626 0.451854 0.330646 -1.501415 -1.518975 0.651663 -0.201506 0.273226
11 -0.390291 1.619411 1.774614 -0.126607 -0.932276 -0.458278 -0.584587 -0.015753 -0.782120 0.726163 0.263520 -1.339206 0.809626 -0.778847 1.100016 0.580454 -1.432560 -1.049585 1.303749 2.839282 -0.137022 0.570998 -0.759602 -0.537312 -1.352517 -0.981209 -0.850375 -0.176332 1.439108 -0.644094 2.131067 0.363364 0.318561 -1.900225 0.246422 -1.621430 -0.198015 -0.895176 -1.080576 -2.086128 -0.198498 2.079183 1.048011 0.692087 1.281942 1.763427 -0.751685 0.005577 -0.303703 0.269054 -1.201835 0.752585 0.309269 1.337928 0.619925 0.108574 0.574482 0.116064 0.309547 -1.301547 -0.670457 0.387271 0.839865 1.325563 0.043316 -1.833903 0.907999 -2.389448 -0.542740 -0.221274 0.109936 0.290798 0.729181 0.129429 0.399911 0.010691 -0.882930 -1.167581 1.344483 -0.968927 -0.652478 -0.245357 -0.581453 0.417568 1.206887 -0.893728 1.124872 -1.013620 -1.787432 1.625260 0.116539 -0.656673 -0.257905 1.637384 -1.208801 1.486140 1.804352 -0.569972 -0.593737 0.320688
12 -0.841958 1.976594 -0.919347 1.665235 0.359623 0.079006 0.314277 0.006462 -0.730992 -0.469349 0.501883 -0.656940 0.474285 1.462439 0.355145 -0.308481 0.800923 -1.356811 0.493128 0.966019 0.493230 1.004594 0.369992 0.202551 -1.073616 -0.013440 -2.010436 -1.805272 0.946989 1.064284 -1.240746 -0.684993 0.135116 0.622696 1.290294 0.098781 -1.243748 -0.738954 0.609780 0.421100 0.860784 -0.595039 -0.791697 0.089656 0.827376 -2.645073 0.105847 -0.005904 0.279245 0.309274 0.058061 -0.534462 -0.034181 0.317847 -2.559706 0.208159 0.216694 -0.665425 -1.418761 0.129536 0.241076 0.016789 -0.541168 -1.020128 0.607081 0.041105 -1.706010 -1.232124 -0.188020 1.044402 -0.205755 0.270556 -0.740398 -0.750627 -0.011121 -1.404668 -0.390065 -0.545630 -1.264436 1.866833 -0.406167 -0.236889 1.180955 -0.203567 -0.483065 0.032342 1.230740 -1.041142 -0.163829 -1.520310 1.130896 -0.962412 1.198897 -0.673820 1.272302 0.165455 -1.299380 -0.168185 0.750180 0.795472
13 -1.607790 -0.135284 1.992852 0.653519 -0.975287 -0.639915 0.895810 -3.010374 -0.352203 0.653947 1.488855 -1.143705 0.851051 1.548766 -0.200701 -0.089069 0.106982 0.425082 1.169581 -2.483634 -1.348812 0.460972 -0.048838 -0.229432 -0.054954 -0.340615 -0.442524 -1.083010 0.272697 -0.450458 0.464978 1.621633 -1.097790 1.851551 -0.982285 -0.498281 1.044486 0.290096 -0.659852 0.887199 2.335015 0.129588 0.925734 0.864578 -0.074303 -0.488976 0.643119 -0.452010 -0.643687 0.916241 1.090533 1.087093 -0.533939 1.221056 0.488828 -0.493243 -0.974390 0.492848 0.048972 1.105719 0.304078 0.786197 -0.192799 1.676440 -0.290316 -0.490248 -1.327119 -0.481945 1.614195 0.274402 0.174576 -0.371581 0.335898 2.302779 0.804725 -1.303485 -1.538532 0.885398 -0.860287 -0.381552 0.915568 1.353958 1.250516 0.061969 0.645419 1.417934 -0.362570 1.132093 1.100105 -0.502228 -0.580007 0.579236 -2.034009 -2.700229 1.219542 0.276008 0.952628 0.653546 1.818918 -0.736245
14 -0.358994 1.766006 1.600749 1.129938 -0.317001 0.136154 -1.443405 1.609132 -0.149346 1.036112 -0.070701 1.672493 0.105572 -0.236833 -0.931379 -1.181006 0.553214 0.643041 -0.666919 -1.453755 0.464123 -0.331426 0.777674 0.892157 0.841378 0.852194 0.142503 2.509879 0.241605 0.306060 -1.247355 -0.795729 -0.989746 -0.202772 -0.626795 0.930808 -0.712120 -0.807966 0.720125 0.576980 -0.589418 -1.643146 -0.575966 0.251417 1.665946 0.348905 0.530798 -0.220368 -0.362805 -0.651722 0.848490 0.197191 1.278370 0.594670 -1.226615 -0.685476 0.087752 -0.322578 -0.388244 0.911859 -0.054691 0.411667 1.018263 -1.115886 -0.324010 -0.345257 0.097191 0.550719 1.791043 -0.649740 -0.490120 0.641850 -1.294050 -0.515439 -0.248534 0.074184 -0.631478 -0.459374 0.157423 1.483868 1.062622 0.806297 1.587836 -0.139304 -1.263802 1.890060 -1.007972 0.159024 0.377539 -0.064761 0.618268 1.508776 -0.441403 -1.036012 -0.181214 0.107254 -0.985868 0.417608 0.382845 -0.279064
15 1.236711 0.341734 -0.261488 -0.069739 -0.388995 0.410353 -0.554708 0.314242 0.550299 0.034209 0.738084 0.777088 -0.696613 -0.605196 1.619734 -0.033292 0.239946 -0.453477 0.858182 0.218731 -0.604270 0.728521 -0.395602 0.229143 -0.142687 0.742699 0.917885 -0.226000 1.694529 -0.974329 -0.106523 2.779497 0.060032 1.384664 -0.058896 0.393177 0.426308 0.242976 -2.754445 1.789714 0.656661 -0.090192 -0.586068 -0.502182 -0.622227 0.723502 -1.771338 -0.149904 0.295554 -1.432124 -1.073264 -0.022111 -1.531465 1.827763 -0.227323 1.827483 -0.977003 0.341793 -0.778863 1.026392 0.043363 -0.036391 0.067151 1.522241 -0.889278 -0.098468 0.819460 -0.507625 0.080827 -1.617682 -0.669991 -1.166801 0.099897 -0.882853 -1.045768 0.737371 -1.093752 -0.291751 -0.385767 0.075584 -1.437895 0.712091 0.209771 2.653311 0.561880 0.551496 0.047194 0.543726 -1.522905 -0.796447 -0.708761 -1.258063 -0.543096 0.728820 -0.599736 0.529403 1.557623 -1.055463 0.610669 -0.126215

It is also possible to stick MultiIndexes and even only specific levels.

[65]:
bigdf.index = pd.MultiIndex.from_product([["A", "B"], [0, 1], [0, 1, 2, 3]])
bigdf.style.set_sticky(axis="index", pixel_size=18, levels=[1, 2])
[65]:
      0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
A 0 0 -0.477904 0.255455 -1.036692 1.133506 -1.503100 0.444102 1.401428 0.290586 -1.783543 1.688557 -0.056869 -1.401320 -2.079290 0.336990 -0.707665 -0.005710 -0.105628 -0.576448 0.033234 1.110586 -0.122865 1.143149 -0.957502 0.921099 0.313698 1.215567 1.332542 -2.057070 1.141553 -0.294125 -1.305428 -0.268712 -0.852149 0.686259 1.407350 0.778940 0.836620 -0.771322 -2.071893 1.247859 0.140568 2.001109 0.840592 0.410528 -0.191591 -0.976692 -0.286939 1.302801 -1.007614 -0.318765 -0.088227 -0.345615 -0.830125 0.731525 0.904993 -0.804781 -0.334342 0.591921 -0.892399 0.173999 -0.987925 0.246079 -1.171178 2.150182 -0.620033 -0.554768 -0.352419 0.409642 0.717860 -0.842420 -0.174826 -2.539582 0.323395 -1.806040 -1.531367 -0.327377 0.866184 0.020804 -0.840086 -0.170454 -0.614758 -1.535173 -1.488820 -1.655628 -0.440489 0.616466 -0.252391 -0.424483 0.334900 -1.231898 -2.024635 -0.840675 0.978826 -1.002244 -0.582052 -0.486294 -0.506214 0.195567 -0.210379 0.738297
1 0.585237 2.647457 0.119200 -0.108335 0.668777 -1.007897 -0.536105 -0.125968 0.494865 -1.690365 0.429727 0.199385 -0.645263 0.017235 -0.645622 -0.673141 0.212909 -0.183091 -0.180927 1.755693 -0.338625 1.355682 0.256700 -0.188953 1.091026 -1.301383 0.973041 0.910566 -0.141481 -1.294820 -0.071232 1.653252 -2.671628 -0.223089 0.987800 0.594616 0.271735 -0.441871 1.141844 -0.104390 -0.782294 -2.443544 1.266727 -0.120827 -0.032833 0.053360 1.156297 0.692768 0.147312 0.949327 0.759757 1.879542 -0.131123 -0.873306 -0.157111 -0.253708 0.593872 -1.079665 1.324434 -1.840771 -0.017790 2.143841 -0.733744 -1.463399 0.103749 0.577465 -0.372991 1.432642 0.698148 0.464842 0.746067 1.341692 -0.030676 1.791347 -0.313512 -0.772671 -1.574769 0.733113 0.667523 -1.295449 -0.329993 -0.098159 1.397084 -0.614709 -2.142259 -0.678900 2.274475 0.082669 0.001121 -0.301682 -1.126572 -1.586037 0.211824 0.250514 0.686721 -0.620784 -0.727293 0.815589 -0.130651 -0.334826
2 -2.275237 -0.161309 -0.090888 -1.069049 1.103881 0.580332 -0.242143 -0.297919 -1.548967 0.424682 0.347525 -1.299632 2.057055 -0.033416 -1.032662 2.532423 -0.139160 -0.381236 0.002564 -0.355384 -0.387259 -0.029480 0.155327 0.019139 0.470035 -0.228955 0.961646 0.455171 2.298854 0.015216 -0.006006 0.942286 -0.075252 0.427614 -0.062118 -0.249663 -0.222687 0.755397 -0.489316 -0.339373 -2.220204 0.450844 1.168297 -1.021234 -0.653084 -0.512592 -0.029187 -0.776034 0.493463 1.018998 1.445182 0.991347 -1.361966 0.676086 -1.814587 -0.610042 -0.300384 0.925241 -0.799652 -0.993661 1.268798 0.848636 -0.265245 -0.186983 -1.820013 -0.466652 0.576109 0.439632 0.393298 -0.221681 -0.486075 0.621923 0.535757 -0.939688 1.043671 0.682593 -0.033550 1.140860 0.902243 -0.802460 -0.081606 -0.801892 1.351038 -0.494487 -0.520726 -0.315359 -0.312007 -0.669371 0.053571 -0.533757 -1.650414 -0.753615 0.398238 0.535282 2.289758 0.269020 -1.048343 0.978725 0.285308 0.221338
3 -0.153361 -0.544615 0.313869 0.571500 0.387451 -1.047070 2.342909 0.258259 -1.251172 0.661437 -1.142217 0.694600 -0.003757 1.737524 -0.754015 -0.088262 1.591698 -0.936549 1.991625 1.460563 0.805654 0.590560 -0.987085 0.971833 0.559503 -0.335600 -0.302770 1.525905 0.393735 -0.257535 0.551972 -0.000964 1.225352 -0.195811 -0.220533 2.850902 -0.540946 -0.141136 0.136409 -1.820779 -0.938299 1.504934 -0.460442 0.511671 0.590275 0.688136 -1.565189 1.332829 0.517363 -0.994249 -0.754060 -0.974907 1.448075 1.194312 0.732741 0.303084 0.424536 -0.696156 -0.190177 -0.706482 1.106542 0.913718 0.065983 0.443318 -1.662877 -0.935094 -1.770420 1.340994 0.037316 -0.158977 0.380143 -0.834214 -0.918589 0.392834 0.919642 -1.374318 0.517009 -0.144410 -0.139062 0.522213 -1.183980 0.783660 1.916785 -1.348197 -1.899979 0.821277 0.127906 1.091722 0.786539 0.679616 2.814977 -0.933216 -1.013307 0.896509 2.743150 0.500607 -0.771423 1.017250 -0.244201 0.777267
1 0 -0.587462 -0.789396 2.061587 -1.159394 1.412129 -0.708493 -2.339050 -1.320658 0.138352 0.547877 1.727875 -0.600047 2.348426 -0.971963 0.997073 1.563061 -0.602184 -0.203174 0.152164 0.456641 -0.229275 2.344958 0.716073 0.814685 -0.614382 1.122271 -0.095403 -0.627311 -0.557301 2.288314 0.875762 -0.063959 -0.226624 -3.506094 0.218051 -0.486305 -0.419128 -0.317432 1.430059 1.450187 -0.320155 1.246194 -0.870016 -0.115043 -0.264790 -1.187109 -0.459220 -1.531756 -2.165223 -0.111649 -0.473090 2.574181 -1.233349 0.450126 1.465904 -0.899823 0.327665 -0.863885 -2.225079 0.101733 -0.547152 -0.998309 -1.173454 -0.215375 -1.665647 0.391019 0.344336 1.741681 -0.863386 -1.304589 0.381078 -0.641358 0.259613 2.602810 0.430121 1.750116 -0.657066 1.376877 0.302335 -0.595907 -0.034496 0.932377 -0.113500 1.068749 -0.149410 0.210923 -0.677550 -0.077921 -0.048069 0.172445 -0.648020 0.734457 0.088277 0.485921 -0.581941 -0.470936 -0.056922 0.534501 -1.352339 -1.175363
1 0.873152 1.601969 0.178049 -1.983827 -0.622169 -0.664296 -1.242833 -1.689207 0.495401 1.720645 -1.126703 -0.405589 0.736988 -1.023278 -1.020098 0.116688 0.866702 1.583894 -0.110518 -1.959511 0.127211 0.309706 0.138433 1.090373 -0.248531 0.838397 -0.213734 -1.341715 0.938799 0.294882 0.068500 -0.878982 0.822640 -0.351942 -0.147947 -0.391392 -1.966654 0.219815 -2.007916 -1.705208 -0.567268 -0.177842 1.851972 1.315402 0.069730 -0.335549 0.572100 0.743115 0.252673 0.779700 -0.003330 -0.262999 0.883145 1.155826 1.220540 0.859195 -0.415208 -1.994984 0.039692 -0.992914 -1.654410 -0.538202 -0.145346 0.009721 0.726617 0.582571 -0.643513 -1.250474 -0.904109 0.695077 -2.134919 -0.755469 -0.744455 0.762004 -0.496951 0.235681 0.621123 0.624283 -0.365994 -0.321027 0.610761 0.731269 -0.096978 1.557334 0.010992 0.954745 -2.251012 -0.066306 0.812171 0.621621 -0.309016 0.517246 -2.180666 -0.832025 0.924467 -1.426500 0.984203 -0.474932 0.175826 0.335576
2 0.433552 -0.201430 -0.943410 -1.042909 0.034170 -1.541850 -1.195631 0.887972 0.393069 -0.958733 -0.072806 0.319061 0.391631 -0.031760 1.371569 -0.040696 0.843700 1.483889 -1.211425 -0.699957 1.422021 0.717107 -0.724958 0.771420 -0.440838 0.761756 2.333486 -1.561506 -0.420946 -0.734936 1.136618 1.606310 0.049381 1.444088 -0.788869 -0.462994 -1.048723 -0.551249 -0.335222 0.402854 -0.804416 -0.638295 0.415961 -0.137121 0.185281 -0.696716 0.113364 1.274840 -0.391330 0.578917 0.430885 0.611646 1.124741 0.868773 0.304308 0.312547 0.315105 -0.917382 -0.187145 0.310147 1.232167 1.489123 0.529581 0.701434 -1.296493 -0.504178 -0.704184 1.080987 -0.124031 0.811502 0.373988 -0.828847 0.423870 0.292172 0.294221 -1.865392 -0.843158 -0.601878 -0.558144 -0.103680 0.126211 -0.448466 0.033401 1.513842 0.027381 -1.746780 0.695925 0.330964 -0.490750 -1.019039 -0.810897 -0.777655 1.109129 -1.257298 0.837863 -1.551229 0.057772 0.002614 1.915058 -0.269061
3 0.474834 0.957474 -0.388375 0.689624 1.383813 -1.470173 -0.149235 -2.435437 1.347520 -0.563205 1.510469 0.594204 -1.060062 1.009238 2.086382 0.764776 0.226951 -1.412304 0.723599 -0.525493 -0.694461 0.459322 -0.633279 -1.406308 1.070413 -1.172406 0.796120 -0.617709 -0.609522 -1.405683 -0.335592 -0.715788 0.745924 -0.578628 0.989271 0.891024 -0.524736 0.150400 -0.280605 -0.309595 -0.090330 0.921847 1.898539 1.930585 -1.439397 -0.054393 -0.319578 -0.081519 -0.217571 -0.129922 -0.977441 0.606411 0.770752 -1.015290 0.709502 -1.402151 0.967668 -0.331202 -1.100451 0.157843 0.146878 0.616650 -1.309539 -0.015751 0.562530 0.332627 -0.613157 -0.923942 0.496891 1.420402 -0.501321 0.902524 -0.049179 1.183360 2.212491 -0.671855 -0.751182 0.246615 0.474175 1.217908 0.281949 0.205916 -1.236634 2.202909 -0.332121 -0.461662 1.414995 0.381989 -0.269530 0.697302 -0.787901 0.816281 -0.489125 0.657600 -1.365946 0.784740 -0.080619 -0.978908 -0.312965 0.890678
B 0 0 0.394178 -0.399837 -0.515396 0.034383 -0.556831 0.401657 -0.678602 -0.199930 -1.609916 0.518976 -1.010074 2.760926 0.404390 -0.799003 -1.868415 2.071176 -0.513190 -0.202935 1.178557 0.157075 -0.250039 1.048015 -0.046891 0.164406 0.508845 -0.126983 1.612909 -0.747256 -1.414439 -0.528527 0.897602 1.274353 -0.982594 0.015758 -0.391586 -1.179800 -1.354369 1.525614 -1.183777 -0.286996 0.421969 -1.455870 -1.225708 0.399306 -1.455237 -0.219310 -0.655527 0.588820 0.894316 0.149485 0.297184 0.494854 -0.225150 -1.232678 -0.395888 1.617737 0.419797 -1.117645 -0.060077 0.775085 0.231908 -1.118163 -0.742908 -0.406036 0.054308 0.418820 -0.777331 -0.536132 -0.420887 0.707553 0.158623 0.717902 0.988025 -0.557313 2.271761 0.462317 1.049660 -0.014489 0.667679 -1.087098 -0.006715 0.117394 -1.049528 0.263727 -0.119058 0.460560 1.376910 -0.754072 0.622208 -0.734321 -1.506515 1.774620 -0.311566 -1.473624 -0.747015 0.319220 -1.373398 -2.811869 -0.613424 0.621426
1 1.671636 -0.038020 1.222513 -0.058518 0.933328 0.800803 0.692282 0.829730 -1.122427 -0.936183 -1.435730 -1.666693 1.022173 2.285226 -1.444257 -1.495789 1.262512 -1.605591 -0.505053 -0.963444 -0.226690 -0.171874 -0.031180 -0.353005 -2.778255 -0.234732 -0.438278 0.720178 1.758388 0.491887 1.081333 0.074762 -1.028180 1.304382 1.312343 -0.087118 -2.581515 -0.077336 1.250050 -0.938518 -0.246761 0.473507 -0.116527 -0.759301 -0.998732 0.738178 0.661729 -0.139460 -0.239646 1.227980 -1.136144 0.262543 -1.418194 -1.282242 0.188574 -0.244128 0.037893 0.531271 0.734701 -0.792197 -0.194756 -0.426177 2.030788 2.307577 -0.755726 2.037830 -1.158194 0.104399 -0.629492 0.489047 0.071801 -2.074276 0.729743 -0.102213 0.011607 0.478700 1.154224 -0.684764 -0.873370 -0.062123 0.099357 1.350460 -0.044939 0.313854 -0.076810 -1.330002 -0.130381 0.356302 0.897122 0.436875 -1.715499 -0.218792 0.396300 -0.099376 1.134847 1.295450 0.434639 1.024399 -1.641434 -0.736609
2 1.082696 0.031717 2.209015 -0.013215 -2.003879 -0.161916 -0.661974 0.560688 -0.930355 -0.510425 -0.578507 -1.647700 0.755420 -0.518368 0.170730 -2.200226 -0.823591 0.465074 -0.464247 -0.099755 0.465379 0.378180 3.238894 -0.776928 -0.881090 -0.453011 0.862192 -0.144060 0.097043 -0.097493 -2.192043 0.138721 -0.865865 -0.791182 0.438490 -0.033203 -1.385018 0.225164 -1.097561 -0.437453 0.197283 -0.899327 0.350646 -0.170051 -0.379607 -0.960015 0.039552 -0.105490 0.807139 -0.599240 -0.069271 -2.185495 0.021436 -0.241760 0.315148 0.833108 -0.557417 -0.482816 0.638546 0.032573 0.334365 0.798180 -0.608151 0.809502 2.421493 0.505470 -2.006477 0.755333 1.045566 0.182879 0.289520 0.729436 0.796884 -1.915597 0.105973 -0.284472 -0.723593 1.055573 0.297566 1.404749 -1.191471 -0.872898 1.131912 0.649634 -0.748611 -0.368370 -1.791016 0.631735 -0.095272 1.651556 -0.108445 0.134226 -0.493626 0.451854 0.330646 -1.501415 -1.518975 0.651663 -0.201506 0.273226
3 -0.390291 1.619411 1.774614 -0.126607 -0.932276 -0.458278 -0.584587 -0.015753 -0.782120 0.726163 0.263520 -1.339206 0.809626 -0.778847 1.100016 0.580454 -1.432560 -1.049585 1.303749 2.839282 -0.137022 0.570998 -0.759602 -0.537312 -1.352517 -0.981209 -0.850375 -0.176332 1.439108 -0.644094 2.131067 0.363364 0.318561 -1.900225 0.246422 -1.621430 -0.198015 -0.895176 -1.080576 -2.086128 -0.198498 2.079183 1.048011 0.692087 1.281942 1.763427 -0.751685 0.005577 -0.303703 0.269054 -1.201835 0.752585 0.309269 1.337928 0.619925 0.108574 0.574482 0.116064 0.309547 -1.301547 -0.670457 0.387271 0.839865 1.325563 0.043316 -1.833903 0.907999 -2.389448 -0.542740 -0.221274 0.109936 0.290798 0.729181 0.129429 0.399911 0.010691 -0.882930 -1.167581 1.344483 -0.968927 -0.652478 -0.245357 -0.581453 0.417568 1.206887 -0.893728 1.124872 -1.013620 -1.787432 1.625260 0.116539 -0.656673 -0.257905 1.637384 -1.208801 1.486140 1.804352 -0.569972 -0.593737 0.320688
1 0 -0.841958 1.976594 -0.919347 1.665235 0.359623 0.079006 0.314277 0.006462 -0.730992 -0.469349 0.501883 -0.656940 0.474285 1.462439 0.355145 -0.308481 0.800923 -1.356811 0.493128 0.966019 0.493230 1.004594 0.369992 0.202551 -1.073616 -0.013440 -2.010436 -1.805272 0.946989 1.064284 -1.240746 -0.684993 0.135116 0.622696 1.290294 0.098781 -1.243748 -0.738954 0.609780 0.421100 0.860784 -0.595039 -0.791697 0.089656 0.827376 -2.645073 0.105847 -0.005904 0.279245 0.309274 0.058061 -0.534462 -0.034181 0.317847 -2.559706 0.208159 0.216694 -0.665425 -1.418761 0.129536 0.241076 0.016789 -0.541168 -1.020128 0.607081 0.041105 -1.706010 -1.232124 -0.188020 1.044402 -0.205755 0.270556 -0.740398 -0.750627 -0.011121 -1.404668 -0.390065 -0.545630 -1.264436 1.866833 -0.406167 -0.236889 1.180955 -0.203567 -0.483065 0.032342 1.230740 -1.041142 -0.163829 -1.520310 1.130896 -0.962412 1.198897 -0.673820 1.272302 0.165455 -1.299380 -0.168185 0.750180 0.795472
1 -1.607790 -0.135284 1.992852 0.653519 -0.975287 -0.639915 0.895810 -3.010374 -0.352203 0.653947 1.488855 -1.143705 0.851051 1.548766 -0.200701 -0.089069 0.106982 0.425082 1.169581 -2.483634 -1.348812 0.460972 -0.048838 -0.229432 -0.054954 -0.340615 -0.442524 -1.083010 0.272697 -0.450458 0.464978 1.621633 -1.097790 1.851551 -0.982285 -0.498281 1.044486 0.290096 -0.659852 0.887199 2.335015 0.129588 0.925734 0.864578 -0.074303 -0.488976 0.643119 -0.452010 -0.643687 0.916241 1.090533 1.087093 -0.533939 1.221056 0.488828 -0.493243 -0.974390 0.492848 0.048972 1.105719 0.304078 0.786197 -0.192799 1.676440 -0.290316 -0.490248 -1.327119 -0.481945 1.614195 0.274402 0.174576 -0.371581 0.335898 2.302779 0.804725 -1.303485 -1.538532 0.885398 -0.860287 -0.381552 0.915568 1.353958 1.250516 0.061969 0.645419 1.417934 -0.362570 1.132093 1.100105 -0.502228 -0.580007 0.579236 -2.034009 -2.700229 1.219542 0.276008 0.952628 0.653546 1.818918 -0.736245
2 -0.358994 1.766006 1.600749 1.129938 -0.317001 0.136154 -1.443405 1.609132 -0.149346 1.036112 -0.070701 1.672493 0.105572 -0.236833 -0.931379 -1.181006 0.553214 0.643041 -0.666919 -1.453755 0.464123 -0.331426 0.777674 0.892157 0.841378 0.852194 0.142503 2.509879 0.241605 0.306060 -1.247355 -0.795729 -0.989746 -0.202772 -0.626795 0.930808 -0.712120 -0.807966 0.720125 0.576980 -0.589418 -1.643146 -0.575966 0.251417 1.665946 0.348905 0.530798 -0.220368 -0.362805 -0.651722 0.848490 0.197191 1.278370 0.594670 -1.226615 -0.685476 0.087752 -0.322578 -0.388244 0.911859 -0.054691 0.411667 1.018263 -1.115886 -0.324010 -0.345257 0.097191 0.550719 1.791043 -0.649740 -0.490120 0.641850 -1.294050 -0.515439 -0.248534 0.074184 -0.631478 -0.459374 0.157423 1.483868 1.062622 0.806297 1.587836 -0.139304 -1.263802 1.890060 -1.007972 0.159024 0.377539 -0.064761 0.618268 1.508776 -0.441403 -1.036012 -0.181214 0.107254 -0.985868 0.417608 0.382845 -0.279064
3 1.236711 0.341734 -0.261488 -0.069739 -0.388995 0.410353 -0.554708 0.314242 0.550299 0.034209 0.738084 0.777088 -0.696613 -0.605196 1.619734 -0.033292 0.239946 -0.453477 0.858182 0.218731 -0.604270 0.728521 -0.395602 0.229143 -0.142687 0.742699 0.917885 -0.226000 1.694529 -0.974329 -0.106523 2.779497 0.060032 1.384664 -0.058896 0.393177 0.426308 0.242976 -2.754445 1.789714 0.656661 -0.090192 -0.586068 -0.502182 -0.622227 0.723502 -1.771338 -0.149904 0.295554 -1.432124 -1.073264 -0.022111 -1.531465 1.827763 -0.227323 1.827483 -0.977003 0.341793 -0.778863 1.026392 0.043363 -0.036391 0.067151 1.522241 -0.889278 -0.098468 0.819460 -0.507625 0.080827 -1.617682 -0.669991 -1.166801 0.099897 -0.882853 -1.045768 0.737371 -1.093752 -0.291751 -0.385767 0.075584 -1.437895 0.712091 0.209771 2.653311 0.561880 0.551496 0.047194 0.543726 -1.522905 -0.796447 -0.708761 -1.258063 -0.543096 0.728820 -0.599736 0.529403 1.557623 -1.055463 0.610669 -0.126215

HTML Escaping#

Suppose you have to display HTML within HTML, that can be a bit of pain when the renderer can’t distinguish. You can use the escape formatting option to handle this, and even use it within a formatter that contains HTML itself.

Note that if you’re using Styler on untrusted, user-provided input to serve HTML then you should escape the input to prevent security vulnerabilities. See the Jinja2 documentation for more.

[66]:
df4 = pd.DataFrame([["<div></div>", '"&other"', "<span></span>"]])
df4.style
[66]:
  0 1 2
0
"&other"
[67]:
df4.style.format(escape="html")
[67]:
  0 1 2
0 <div></div> "&other" <span></span>
[68]:
df4.style.format(
    '<a href="https://pandas.pydata.org" target="_blank">{}</a>', escape="html"
)

Export to Excel#

Some support (since version 0.20.0) is available for exporting styled DataFrames to Excel worksheets using the OpenPyXL or XlsxWriter engines. CSS2.2 properties handled include:

  • background-color

  • border-style properties

  • border-width properties

  • border-color properties

  • color

  • font-family

  • font-style

  • font-weight

  • text-align

  • text-decoration

  • vertical-align

  • white-space: nowrap

  • Shorthand and side-specific border properties are supported (e.g. border-style and border-left-style) as well as the border shorthands for all sides (border: 1px solid green) or specified sides (border-left: 1px solid green). Using a border shorthand will override any border properties set before it (See CSS Working Group for more details)

  • Only CSS2 named colors and hex colors of the form #rgb or #rrggbb are currently supported.

  • The following pseudo CSS properties are also available to set Excel specific style properties:

    • number-format

    • border-style (for Excel-specific styles: “hair”, “mediumDashDot”, “dashDotDot”, “mediumDashDotDot”, “dashDot”, “slantDashDot”, or “mediumDashed”)

Table level styles, and data cell CSS-classes are not included in the export to Excel: individual cells must have their properties mapped by the Styler.apply and/or Styler.map methods.

[69]:
df2.style.map(style_negative, props="color:red;").highlight_max(axis=0).to_excel(
    "styled.xlsx", engine="openpyxl"
)

A screenshot of the output:

Excel spreadsheet with styled DataFrame

Export to LaTeX#

There is support (since version 1.3.0) to export Styler to LaTeX. The documentation for the .to_latex method gives further detail and numerous examples.

More About CSS and HTML#

Cascading Style Sheet (CSS) language, which is designed to influence how a browser renders HTML elements, has its own peculiarities. It never reports errors: it just silently ignores them and doesn’t render your objects how you intend so can sometimes be frustrating. Here is a very brief primer on how Styler creates HTML and interacts with CSS, with advice on common pitfalls to avoid.

CSS Classes and Ids#

The precise structure of the CSS class attached to each cell is as follows.

  • Cells with Index and Column names include index_name and level<k> where k is its level in a MultiIndex

  • Index label cells include

    • row_heading

    • level<k> where k is the level in a MultiIndex

    • row<m> where m is the numeric position of the row

  • Column label cells include

    • col_heading

    • level<k> where k is the level in a MultiIndex

    • col<n> where n is the numeric position of the column

  • Data cells include

    • data

    • row<m>, where m is the numeric position of the cell.

    • col<n>, where n is the numeric position of the cell.

  • Blank cells include blank

  • Trimmed cells include col_trim or row_trim

The structure of the id is T_uuid_level<k>_row<m>_col<n> where level<k> is used only on headings, and headings will only have either row<m> or col<n> whichever is needed. By default 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 read more about the use of UUIDs in Optimization.

We can see example of the HTML by calling the .to_html() method.

[70]:
print(
    pd.DataFrame(
        [[1, 2], [3, 4]], index=["i1", "i2"], columns=["c1", "c2"]
    ).style.to_html()
)
<style type="text/css">
</style>
<table id="T_c3533">
  <thead>
    <tr>
      <th class="blank level0" >&nbsp;</th>
      <th id="T_c3533_level0_col0" class="col_heading level0 col0" >c1</th>
      <th id="T_c3533_level0_col1" class="col_heading level0 col1" >c2</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th id="T_c3533_level0_row0" class="row_heading level0 row0" >i1</th>
      <td id="T_c3533_row0_col0" class="data row0 col0" >1</td>
      <td id="T_c3533_row0_col1" class="data row0 col1" >2</td>
    </tr>
    <tr>
      <th id="T_c3533_level0_row1" class="row_heading level0 row1" >i2</th>
      <td id="T_c3533_row1_col0" class="data row1 col0" >3</td>
      <td id="T_c3533_row1_col1" class="data row1 col1" >4</td>
    </tr>
  </tbody>
</table>

CSS Hierarchies#

The examples have shown that when CSS styles overlap, the one that comes last in the HTML render, takes precedence. So the following yield different results:

[71]:
df4 = pd.DataFrame([["text"]])
df4.style.map(lambda x: "color:green;").map(lambda x: "color:red;")
[71]:
  0
0 text
[72]:
df4.style.map(lambda x: "color:red;").map(lambda x: "color:green;")
[72]:
  0
0 text

This is only true for CSS rules that are equivalent in hierarchy, or importance. You can read more about CSS specificity here but for our purposes it suffices to summarize the key points:

A CSS importance score for each HTML element is derived by starting at zero and adding:

  • 1000 for an inline style attribute

  • 100 for each ID

  • 10 for each attribute, class or pseudo-class

  • 1 for each element name or pseudo-element

Let’s use this to describe the action of the following configurations

[73]:
df4.style.set_uuid("a_").set_table_styles(
    [{"selector": "td", "props": "color:red;"}]
).map(lambda x: "color:green;")
[73]:
  0
0 text

This text is red because the generated selector #T_a_ td is worth 101 (ID plus element), whereas #T_a_row0_col0 is only worth 100 (ID), so is considered inferior even though in the HTML it comes after the previous.

[74]:
df4.style.set_uuid("b_").set_table_styles(
    [
        {"selector": "td", "props": "color:red;"},
        {"selector": ".cls-1", "props": "color:blue;"},
    ]
).map(lambda x: "color:green;").set_td_classes(pd.DataFrame([["cls-1"]]))
[74]:
  0
0 text

In the above case the text is blue because the selector #T_b_ .cls-1 is worth 110 (ID plus class), which takes precedence.

[75]:
df4.style.set_uuid("c_").set_table_styles(
    [
        {"selector": "td", "props": "color:red;"},
        {"selector": ".cls-1", "props": "color:blue;"},
        {"selector": "td.data", "props": "color:yellow;"},
    ]
).map(lambda x: "color:green;").set_td_classes(pd.DataFrame([["cls-1"]]))
[75]:
  0
0 text

Now we have created another table style this time the selector T_c_ td.data (ID plus element plus class) gets bumped up to 111.

If your style fails to be applied, and its really frustrating, try the !important trump card.

[76]:
df4.style.set_uuid("d_").set_table_styles(
    [
        {"selector": "td", "props": "color:red;"},
        {"selector": ".cls-1", "props": "color:blue;"},
        {"selector": "td.data", "props": "color:yellow;"},
    ]
).map(lambda x: "color:green !important;").set_td_classes(pd.DataFrame([["cls-1"]]))
[76]:
  0
0 text

Finally got that green text after all!

Extensibility#

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.

Subclassing#

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.

[77]:
from jinja2 import Environment, ChoiceLoader, FileSystemLoader
from IPython.display import HTML
from pandas.io.formats.style import Styler

We’ll use the following template:

[78]:
with open("templates/myhtml.tpl") as f_html:
    print(f_html.read())
{% extends "html_table.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.

[79]:
class MyStyler(Styler):
    env = Environment(
        loader=ChoiceLoader(
            [
                FileSystemLoader("templates"),  # contains ours
                Styler.loader,  # the default
            ]
        )
    )
    template_html_table = 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.

[80]:
MyStyler(df3)
[80]:

My Table

    c1 c2 c3 c4
A r1 0.472962 -0.713451 0.547370 1.710349
r2 -0.858880 -0.417555 -0.593869 -0.320305
B r1 -0.171749 0.843842 0.013460 1.320816
r2 -0.837307 1.022460 1.024871 -0.348727

Our custom template accepts a table_title keyword. We can provide the value in the .to_html method.

[81]:
HTML(MyStyler(df3).to_html(table_title="Extending Example"))
[81]:

Extending Example

    c1 c2 c3 c4
A r1 0.472962 -0.713451 0.547370 1.710349
r2 -0.858880 -0.417555 -0.593869 -0.320305
B r1 -0.171749 0.843842 0.013460 1.320816
r2 -0.837307 1.022460 1.024871 -0.348727

For convenience, we provide the Styler.from_custom_template method that does the same as the custom subclass.

[82]:
EasyStyler = Styler.from_custom_template("templates", "myhtml.tpl")
HTML(EasyStyler(df3).to_html(table_title="Another Title"))
[82]:

Another Title

    c1 c2 c3 c4
A r1 0.472962 -0.713451 0.547370 1.710349
r2 -0.858880 -0.417555 -0.593869 -0.320305
B r1 -0.171749 0.843842 0.013460 1.320816
r2 -0.837307 1.022460 1.024871 -0.348727

Template Structure#

Here’s the template structure for the both the style generation template and the table generation template:

Style template:

[84]:
HTML(style_structure)
[84]:
before_style
style
<style type="text/css">
table_styles
before_cellstyle
cellstyle
</style>

Table template:

[86]:
HTML(table_structure)
[86]:
before_table
table
<table ...>
caption
thead
before_head_rows
head_tr (loop over headers)
after_head_rows
tbody
before_rows
tr (loop over data rows)
after_rows
</table>
after_table

See the template in the GitHub repo for more details.