pandas.io.formats.style.Styler.map_index #
- 造型器。map_index ( func , axis = 0 , level = None , ** kwargs ) [来源] #
按元素将 CSS 样式函数应用于索引或列标题。
用结果更新 HTML 表示。
1.4.0 版本中的新增功能。
版本 2.1.0 中的新增功能: Styler.applymap_index 已弃用并重命名为 Styler.map_index。
- 参数:
- 函数函数
func
应该接受一个标量并返回一个字符串。- 轴{0, 1, “索引”, “列”}
应用函数的标头。
- level int、str、列表、可选
如果索引是 MultiIndex 则应用该函数的级别。
- **夸格斯字典
传递到
func
.
- 返回:
- 造型器
也可以看看
Styler.apply_index
将 CSS 样式函数逐级应用于标题。
Styler.apply
按列、按行或按表应用 CSS 样式函数。
Styler.map
按元素应用 CSS 样式函数。
笔记
每个输入都
func
将是一个索引值(如果是索引)或多重索引的级别值。的输出func
应该是 CSS 样式字符串,格式为 'attribute: value;属性2:值2; ...' 或者,如果没有任何内容应用于该元素,则为空字符串或None
.例子
有条件地突出显示索引中的值的基本用法。
>>> df = pd.DataFrame([[1,2], [3,4]], index=["A", "B"]) >>> def color_b(s): ... return "background-color: yellow;" if v == "B" else None >>> df.style.map_index(color_b)
有选择地应用于特定级别的多索引列。
>>> midx = pd.MultiIndex.from_product([['ix', 'jy'], [0, 1], ['x3', 'z4']]) >>> df = pd.DataFrame([np.arange(8)], columns=midx) >>> def highlight_x(v): ... return "background-color: yellow;" if "x" in v else None >>> df.style.map_index(highlight_x, axis="columns", level=[0, 2]) ...