Skip to main content
`Ctrl`+`K`

# pandas.io.formats.style.Styler.map_index

Styler.map_index(func, axis=0, level=None, **kwargs)

  Apply a CSS-styling function to the index or column headers, elementwise.

  Updates the HTML representation with the result.

  Added in version 2.1.0: Styler.applymap_index was deprecated and renamed to Styler.map_index.

  Parameters:
    func function

      `func` should take a scalar and return a string.

    axis{{0, 1, “index”, “columns”}}

      The headers over which to apply the function.

    level int, str, list, optional

      If index is MultiIndex the level(s) over which to apply the function.

    **kwargs dict

      Pass along to `func`.

  Returns:
    Styler

      Instance of class with CSS applied to its HTML representation.

  See also

  `Styler.apply_index`

    Apply a CSS-styling function to headers level-wise.

  `Styler.apply`

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

  `Styler.map`

    Apply a CSS-styling function elementwise.

  Notes

  Each input to `func` will be an index value, if an Index, or a level value of a MultiIndex. The output of `func` should be CSS styles as a string, 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(label):
      ...     return "background-color: yellow;" if label == "B" else None
      >>> df.style.map_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(label):
      ...     return "background-color: yellow;" if "x" in label else None
      >>> df.style.map_index(
      ...     highlight_x, axis="columns", level=[0, 2]
      ... )