pandas.io.formats.style.Styler.highlight_between — pandas 2.2.3 documentation (original) (raw)

Styler.highlight_between(subset=None, color='yellow', axis=0, left=None, right=None, inclusive='both', props=None)[source]#

Highlight a defined range with a style.

Added in version 1.3.0.

Parameters:

subsetlabel, array-like, IndexSlice, optional

A valid 2d input to DataFrame.loc[], or, in the case of a 1d input or single key, to DataFrame.loc[:, ] where the columns are prioritised, to limit data to before applying the function.

colorstr, default ‘yellow’

Background color to use for highlighting.

axis{0 or ‘index’, 1 or ‘columns’, None}, default 0

If left or right given as sequence, axis along which to apply those boundaries. See examples.

leftscalar or datetime-like, or sequence or array-like, default None

Left bound for defining the range.

rightscalar or datetime-like, or sequence or array-like, default None

Right bound for defining the range.

inclusive{‘both’, ‘neither’, ‘left’, ‘right’}

Identify whether bounds are closed or open.

propsstr, default None

CSS properties to use for highlighting. If props is given, coloris not used.

Returns:

Styler

Notes

If left is None only the right bound is applied. If right is None only the left bound is applied. If both are Noneall values are highlighted.

axis is only needed if left or right are provided as a sequence or an array-like object for aligning the shapes. If left and right are both scalars then all axis inputs will give the same result.

This function only works with compatible dtypes. For example a datetime-like region can only use equivalent datetime-like left and right arguments. Use subset to control regions which have multiple dtypes.

Examples

Basic usage

df = pd.DataFrame({ ... 'One': [1.2, 1.6, 1.5], ... 'Two': [2.9, 2.1, 2.5], ... 'Three': [3.1, 3.2, 3.8], ... }) df.style.highlight_between(left=2.1, right=2.9)

../../_images/hbetw_basic.png

Using a range input sequence along an axis, in this case setting a leftand right for each column individually

df.style.highlight_between(left=[1.4, 2.4, 3.4], right=[1.6, 2.6, 3.6], ... axis=1, color="#fffd75")

../../_images/hbetw_seq.png

Using axis=None and providing the left argument as an array that matches the input DataFrame, with a constant right

df.style.highlight_between(left=[[2,2,3],[2,2,3],[3,3,3]], right=3.5, ... axis=None, color="#fffd75")

../../_images/hbetw_axNone.png

Using props instead of default background coloring

df.style.highlight_between(left=1.5, right=3.5, ... props='font-weight:bold;color:#e83e8c')

../../_images/hbetw_props.png