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
condcallable

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

valuestr

Applied when cond returns true.

otherstr

Applied when cond returns false.

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.

**kwargsdict

Pass along to cond.

Returns
selfStyler

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;')