Object to define how values are displayed. See notes.
subsetlabel, array-like, IndexSlice, optional
A valid 2d input to DataFrame.loc[<subset>], or, in the case of a 1d input
or single key, to DataFrame.loc[:, <subset>] where the columns are
prioritised, to limit data to before applying the function.
na_repstr, optional
Representation for missing values.
If na_rep is None, no special formatting is applied.
precisionint, optional
Floating point precision to use for display purposes, if not determined by
the specified formatter.
Added in version 1.3.0.
decimalstr, default "."
Character used as decimal separator for floats, complex and integers.
Added in version 1.3.0.
thousandsstr, optional, default None
Character used as thousands separator for floats, complex and integers.
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 \). Escaping is done before formatter.
Added in version 1.3.0.
hyperlinks{"html", "latex"}, optional
Convert string patterns containing https://, http://, ftp:// or www. to
HTML <a> tags as clickable URL hyperlinks if "html", or LaTeX href
commands if "latex".
This method assigns a formatting function, formatter, to each cell in the
DataFrame. If formatter is None, then the default formatter is used.
If a callable then that function should take a data value as input and return
a displayable representation, such as a string. If formatter is
given as a string this is assumed to be a valid Python format specification
and is wrapped to a callable as string.format(x). If a dict is given,
keys should correspond to column names, and values should be string or
callable, as above.
The default formatter currently expresses floats and complex numbers with the
pandas display precision unless using the precision argument here. The
default formatter does not adjust the representation of missing values unless
the na_rep argument is used.
The subset argument defines which region to apply the formatting function
to. If the formatter argument is given in dict form but does not include
all columns within the subset then these columns will have the default formatter
applied. Any columns in the formatter dict excluded from the subset will
be ignored.
When using a formatter string the dtypes must be compatible, otherwise a
ValueError will be raised.
When instantiating a Styler, default formatting can be applied be setting the
pandas.options:
styler.format.formatter: default None.
styler.format.na_rep: default None.
styler.format.precision: default 6.
styler.format.decimal: default ".".
styler.format.thousands: default None.
styler.format.escape: default None.
Warning
Styler.format is ignored when using the output format Styler.to_excel,
since Excel and Python have inherrently different formatting structures.
However, it is possible to use the number-format pseudo CSS attribute
to force Excel permissible formatting. See examples.
Examples
Using na_rep and precision with the default formatter
>>> df=pd.DataFrame([[np.nan,1.0,'A'],[2.0,np.nan,3.0]])>>> df.style.format(na_rep='MISS',precision=3) 0 1 20 MISS 1.000 A1 2.000 MISS 3.000
Using a formatter specification on consistent column dtypes
>>> df.style.format('{:.2f}',na_rep='MISS',subset=[0,1]) 0 1 20 MISS 1.00 A1 2.00 MISS 3.000000
Using the default formatter for unspecified columns
>>> df.style.format({0:'{:.2f}',1:'£ {:.1f}'},na_rep='MISS',precision=1)... 0 1 20 MISS £ 1.0 A1 2.00 MISS 3.0
Multiple na_rep or precision specifications under the default
formatter.
Pandas defines a number-format pseudo CSS attribute instead of the .format
method to create to_excel permissible formatting. Note that semi-colons are
CSS protected characters but used as separators in Excel’s format string.
Replace semi-colons with the section separator character (ASCII-245) when
defining the formatting here.