pandas.io.formats.style.Styler.set_tooltips#

Styler.set_tooltips(ttips, props=None, css_class=None, as_title_attribute=False)[source]#

Set the DataFrame of strings on Styler generating :hover tooltips.

These string based tooltips are only applicable to <td> HTML elements, and cannot be used for column or index headers.

Added in version 1.3.0.

Parameters:
ttipsDataFrame

DataFrame containing strings that will be translated to tooltips, mapped by identical column and index values that must exist on the underlying Styler data. None, NaN values, and empty strings will be ignored and not affect the rendered HTML.

propslist-like or str, optional

List of (attr, value) tuples or a valid CSS string. If None adopts the internal default values described in notes.

css_classstr, optional

Name of the tooltip class used in CSS, should conform to HTML standards. Only useful if integrating tooltips with external CSS. If None uses the internal default value ‘pd-t’.

as_title_attributebool, default False

Add the tooltip text as title attribute to resultant <td> element. If True then props and css_class arguments are ignored.

Returns:
Styler
Instance of class with DataFrame set for strings on Styler

generating :hover tooltips.

See also

Styler.set_table_attributes

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

Styler.set_table_styles

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

Notes

Tooltips are created by adding <span class=”pd-t”></span> to each data cell and then manipulating the table level CSS to attach pseudo hover and pseudo after selectors to produce the required the results.

The default properties for the tooltip CSS class are:

  • visibility: hidden

  • position: absolute

  • z-index: 1

  • background-color: black

  • color: white

  • transform: translate(-20px, -20px)

The property ‘visibility: hidden;’ is a key prerequisite to the hover functionality, and should always be included in any manual properties specification, using the props argument.

Tooltips are not designed to be efficient, and can add large amounts of additional HTML for larger tables, since they also require that cell_ids is forced to True.

If multiline tooltips are required, or if styling is not required and/or space is of concern, then utilizing as_title_attribute as True will store the tooltip on the <td> title attribute. This will cause no CSS to be generated nor will the <span> elements. Storing tooltips through the title attribute will mean that tooltip styling effects do not apply.

Examples

Basic application

>>> df = pd.DataFrame(data=[[0, 1], [2, 3]])
>>> ttips = pd.DataFrame(
...     data=[["Min", ""], [np.nan, "Max"]], columns=df.columns, index=df.index
... )
>>> s = df.style.set_tooltips(ttips).to_html()

Optionally controlling the tooltip visual display

>>> df.style.set_tooltips(
...     ttips,
...     css_class="tt-add",
...     props=[
...         ("visibility", "hidden"),
...         ("position", "absolute"),
...         ("z-index", 1),
...     ],
... )  
>>> df.style.set_tooltips(
...     ttips,
...     css_class="tt-add",
...     props="visibility:hidden; position:absolute; z-index:1;",
... )
... 

Multiline tooltips with smaller size footprint

>>> df.style.set_tooltips(ttips, as_title_attribute=True)