pandas.io.formats.style.Styler.where

Styler.where(cond, value, other=None, subset=None, **kwargs)[source]

Apply CSS-styles based on a conditional function elementwise.

Deprecated since version 1.3.0.

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

Parameters
cond:callable

cond should take a scalar, and optional keyword arguments, and return a boolean.

value:str

Applied when cond returns true.

other:str

Applied when cond returns false.

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 cond.

Returns
self:Styler

See also

Styler.applymap

Apply a CSS-styling function elementwise.

Styler.apply

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

Notes

This method is deprecated.

This method is a convenience wrapper for Styler.applymap(), which we recommend using instead.

The example:

>>> df = pd.DataFrame([[1, 2], [3, 4]])
>>> def cond(v, limit=4):
...     return v > 1 and v != limit
>>> df.style.where(cond, value='color:green;', other='color:red;')

should be refactored to:

>>> def style_func(v, value, other, limit=4):
...     cond = v > 1 and v != limit
...     return value if cond else other
>>> df.style.applymap(style_func, value='color:green;', other='color:red;')

© 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.where.html