pandas.io.formats.style.Styler.map#
- Styler.map(func, subset=None, **kwargs)[源代码]#
逐元素应用 CSS 样式函数。
使用应用结果更新 HTML 表示。
- Parameters:
- funcfunction
func应接受一个标量并返回一个字符串。- subset标签、类数组、IndexSlice、可选
DataFrame.loc[<subset>] 的有效二维输入,或者,在输入为一维或单个键的情况下,为 DataFrame.loc[:, <subset>],其中列优先,用于在应用函数 之前 限制
data。- **kwargsdict
传递给
func。
- Returns:
- Styler
参见
Styler.map_index逐个元素地将 CSS 样式函数应用于标头。
Styler.apply_index逐级别地将 CSS 样式函数应用于标头。
Styler.apply按列、按行或按表应用 CSS 样式函数。
Notes
func的输出元素应该是 CSS 样式字符串,格式为 ‘attribute: value; attribute2: value2; …’,或者,如果没有要应用于该元素的样式,则为空字符串或None。Examples
>>> def color_negative(v, color): ... return f"color: {color};" if v < 0 else None >>> df = pd.DataFrame(np.random.randn(5, 2), columns=["A", "B"]) >>> df.style.map(color_negative, color='red')
使用
subset将应用限制为单个列或多个列。>>> df.style.map(color_negative, color='red', subset="A") ... >>> df.style.map(color_negative, color='red', subset=["A", "B"]) ...
使用二维输入
subset来选择除了列之外的行。>>> df.style.map(color_negative, color='red', ... subset=([0,1,2], slice(None))) >>> df.style.map(color_negative, color='red', subset=(slice(0,5,2), "A")) ...
更多详情请参阅 Table Visualization 用户指南。