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
funcfunction

func should take a scalar and return a scalar.

subsetIndexSlice

A valid indexer to limit data to before applying the function. Consider using a pandas.IndexSlice.

**kwargsdict

Pass along to func.

Returns
selfStyler

See also

Styler.where

Updates the HTML representation with a style which is selected in accordance with the return value of a function.

Notes

The output of func should be a CSS style as string or, if nothing is to be applied, 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))
>>> df.style.applymap(color_negative, color='red')