pandas.io.formats.style.Styler.format¶
- Styler.format(formatter=None, subset=None, na_rep=None, precision=None, decimal='.', thousands=None, escape=None)[source]¶
Format the text display value of cells.
- Parameters
- formatterstr, callable, dict or None
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.New in version 1.0.0.
- precisionint, optional
Floating point precision to use for display purposes, if not determined by the specified
formatter
.New in version 1.3.0.
- decimalstr, default “.”
Character used as decimal separator for floats, complex and integers
New in version 1.3.0.
- thousandsstr, optional, default None
Character used as thousands separator for floats, complex and integers
New 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. Escaping is done beforeformatter
.New in version 1.3.0.
- Returns
- selfStyler
Notes
This method assigns a formatting function,
formatter
, to each cell in the DataFrame. Ifformatter
isNone
, 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. Ifformatter
is given as a string this is assumed to be a valid Python format specification and is wrapped to a callable asstring.format(x)
. If adict
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 thena_rep
argument is used.The
subset
argument defines which region to apply the formatting function to. If theformatter
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.Examples
Using
na_rep
andprecision
with the defaultformatter
>>> df = pd.DataFrame([[np.nan, 1.0, 'A'], [2.0, np.nan, 3.0]]) >>> df.style.format(na_rep='MISS', precision=3) 0 1 2 0 MISS 1.000 A 1 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 2 0 MISS 1.00 A 1 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 2 0 MISS £ 1.0 A 1 2.00 MISS 3.0
Multiple
na_rep
orprecision
specifications under the defaultformatter
.>>> df.style.format(na_rep='MISS', precision=1, subset=[0]) ... .format(na_rep='PASS', precision=2, subset=[1, 2]) 0 1 2 0 MISS 1.00 A 1 2.0 PASS 3.00
Using a callable
formatter
function.>>> func = lambda s: 'STRING' if isinstance(s, str) else 'FLOAT' >>> df.style.format({0: '{:.1f}', 2: func}, precision=4, na_rep='MISS') 0 1 2 0 MISS 1.0000 STRING 1 2.0 MISS FLOAT
Using a
formatter
with HTMLescape
andna_rep
.>>> df = pd.DataFrame([['<div></div>', '"A&B"', None]]) >>> s = df.style.format( ... '<a href="a.com/{0}">{0}</a>', escape="html", na_rep="NA" ... ) >>> s.render() ... <td .. ><a href="a.com/<div></div>"><div></div></a></td> <td .. ><a href="a.com/"A&B"">"A&B"</a></td> <td .. >NA</td> ...
Using a
formatter
with LaTeXescape
.>>> df = pd.DataFrame([["123"], ["~ ^"], ["$%#"]]) >>> s = df.style.format("\\textbf{{{}}}", escape="latex").to_latex() \begin{tabular}{ll} {} & {0} \\ 0 & \textbf{123} \\ 1 & \textbf{\textasciitilde \space \textasciicircum } \\ 2 & \textbf{\$\%\#} \\ \end{tabular}