Apply a CSS-styling function to the index or column headers, level-wise.
Updates the HTML representation with the result.
Added in version 1.4.0.
Added in version 2.1.0: Styler.applymap_index was deprecated and renamed to Styler.map_index.
func should take a Series and return a string array of the same length.
The headers over which to apply the function.
If index is MultiIndex the level(s) over which to apply the function.
Pass along to func.
See also
Styler.map_indexApply a CSS-styling function to headers elementwise.
Styler.applyApply a CSS-styling function column-wise, row-wise, or table-wise.
Styler.mapApply a CSS-styling function elementwise.
Notes
Each input to func will be the index as a Series, if an Index, or a level of a MultiIndex. The output of func should be an identically sized array of 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
Basic usage to conditionally highlight values in the index.
>>> df = pd.DataFrame([[1,2], [3,4]], index=["A", "B"])
>>> def color_b(s):
... return np.where(s == "B", "background-color: yellow;", "")
>>> df.style.apply_index(color_b)
Selectively applying to specific levels of MultiIndex columns.
>>> midx = pd.MultiIndex.from_product([['ix', 'jy'], [0, 1], ['x3', 'z4']])
>>> df = pd.DataFrame([np.arange(8)], columns=midx)
>>> def highlight_x(s):
... return ["background-color: yellow;" if "x" in v else "" for v in s]
>>> df.style.apply_index(highlight_x, axis="columns", level=[0, 2])
...
© 2008–2011, AQR Capital Management, LLC, Lambda Foundry, Inc. and PyData Development Team
© 2011–2025, Open source contributors
Licensed under the 3-clause BSD License.
https://pandas.pydata.org/pandas-docs/version/2.3.0/reference/api/pandas.io.formats.style.Styler.apply_index.html