pandas.io.formats.style.Styler — pandas 3.0.0.dev0+2100.gf496acffcc documentation (original) (raw)

class pandas.io.formats.style.Styler(data, precision=None, table_styles=None, uuid=None, caption=None, table_attributes=None, cell_ids=True, na_rep=None, uuid_len=5, decimal=None, thousands=None, escape=None, formatter=None)[source]#

Helps style a DataFrame or Series according to the data with HTML and CSS.

This class provides methods for styling and formatting a Pandas DataFrame or Series. The styled output can be rendered as HTML or LaTeX, and it supports CSS-based styling, allowing users to control colors, font styles, and other visual aspects of tabular data. It is particularly useful for presenting DataFrame objects in a Jupyter Notebook environment or when exporting styled tables for reports and

Parameters:

dataSeries or DataFrame

Data to be styled - either a Series or DataFrame.

precisionint, optional

Precision to round floats to. If not given defaults topandas.options.styler.format.precision.

Changed in version 1.4.0.

table_styleslist-like, default None

List of {selector: (attr, value)} dicts; see Notes.

uuidstr, default None

A unique identifier to avoid CSS collisions; generated automatically.

captionstr, tuple, default None

String caption to attach to the table. Tuple only used for LaTeX dual captions.

table_attributesstr, default None

Items that show up in the opening <table> tag in addition to automatic (by default) id.

cell_idsbool, default True

If True, each cell will have an id attribute in their HTML tag. The id takes the form T_<uuid>_row<num_row>_col<num_col>where <uuid> is the unique identifier, <num_row> is the row number and <num_col> is the column number.

na_repstr, optional

Representation for missing values. If na_rep is None, no special formatting is applied, and falls back topandas.options.styler.format.na_rep.

uuid_lenint, default 5

If uuid is not specified, the length of the uuid to randomly generate expressed in hex characters, in range [0, 32].

decimalstr, optional

Character used as decimal separator for floats, complex and integers. If not given uses pandas.options.styler.format.decimal.

Added in version 1.3.0.

thousandsstr, optional, default None

Character used as thousands separator for floats, complex and integers. If not given uses pandas.options.styler.format.thousands.

Added in version 1.3.0.

escapestr, optional

Use ‘html’ to replace the characters &, <, >, ', and "in cell display string with HTML-safe sequences.

Use ‘latex’ to replace the characters &, %, $, #, _,{, }, ~, ^, and \ in the cell display string with LaTeX-safe sequences. Use ‘latex-math’ to replace the characters the same way as in ‘latex’ mode, except for math substrings, which either are surrounded by two characters $ or start with the character \( and end with \). If not given uses pandas.options.styler.format.escape.

Added in version 1.3.0.

formatterstr, callable, dict, optional

Object to define how values are displayed. See Styler.format. If not given uses pandas.options.styler.format.formatter.

Added in version 1.4.0.

Attributes

See also

DataFrame.style

Return a Styler object containing methods for building a styled HTML representation for the DataFrame.

Notes

Warning

Styler is primarily intended for use on safe input that you control. When using Styler on untrusted, user-provided input to serve HTML, you should set escape="html" to prevent security vulnerabilities. See the Jinja2 documentation on escaping HTML for more.

Most styling will be done by passing style functions intoStyler.apply or Styler.map. Style functions should return values with strings containing CSS 'attr: value' that will be applied to the indicated cells.

If using in the Jupyter notebook, Styler has defined a _repr_html_to automatically render itself. Otherwise call Styler.to_html to get the generated HTML.

CSS classes are attached to the generated HTML

Any, or all, or these classes can be renamed by using the css_class_namesargument in Styler.set_table_styles, giving a value such as_{“row”: “MY_ROW_CLASS”, “col_trim”: “”, “row_trim”: “”}_.

Examples

df = pd.DataFrame( ... [[1.0, 2.0, 3.0], [4, 5, 6]], index=["a", "b"], columns=["A", "B", "C"] ... ) pd.io.formats.style.Styler( ... df, precision=2, caption="My table" ... )

Please see:Table Visualization for more examples.

Attributes

Methods