pandas.io.formats.style.Styler.applymap

Styler.applymap(func, subset=None, **kwargs)[source]

Apply a CSS-styling function elementwise.

Updates the HTML representation with the result.

Parameters
func:function

func should take a scalar and return a scalar.

subset:label, 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.

**kwargs:dict

Pass along to func.

Returns
self:Styler

See also

Styler.apply

Apply a CSS-styling function column-wise, row-wise, or table-wise.

Notes

The elements of the output of func should be CSS styles as strings, in the format ‘attribute: value; attribute2: value2; …’ or, if nothing is to be applied to that element, an empty string or None.

Examples

>>> def color_negative(v, color):
...     return f"color: {color};" if v < 0 else None
>>> df = pd.DataFrame(np.random.randn(5, 2), columns=["A", "B"])
>>> df.style.applymap(color_negative, color='red')

Using subset to restrict application to a single column or multiple columns

>>> df.style.applymap(color_negative, color='red', subset="A")
>>> df.style.applymap(color_negative, color='red', subset=["A", "B"])

Using a 2d input to subset to select rows in addition to columns

>>> df.style.applymap(color_negative, color='red', subset=([0,1,2], slice(None))
>>> df.style.applymap(color_negative, color='red', subset=(slice(0,5,2), "A")

© 2008–2021, AQR Capital Management, LLC, Lambda Foundry, Inc. and PyData Development Team
Licensed under the 3-clause BSD License.
https://pandas.pydata.org/pandas-docs/version/1.3.4/reference/api/pandas.io.formats.style.Styler.applymap.html