用法:
Styler.apply(func, axis=0, subset=None, **kwargs)按列、按行或table-wise 应用CSS-styling 函数。
用结果更新 HTML 表示。
- func:函数
func如果在 [0,1] 中的axis应采用 Series 并返回相同长度的 list-like 对象,或返回序列,不一定具有相同长度,考虑到subset具有有效的索引标签。func如果axis是None则应该采用 DataFrame 并返回具有相同形状的 ndarray 或 DataFrame,不一定具有相同的形状,考虑到subset具有有效的索引和列标签。- axis:{0 或 ‘index’,1 或 ‘columns’,无},默认 0
适用于每一列(
axis=0或'index')、每一行(axis=1或'columns'),或使用axis=None一次应用于整个 DataFrame。- subset:标签,array-like,IndexSlice,可选
DataFrame.loc[<subset>]的有效 2d 输入,或者在 1d 输入或单键的情况下,到列优先的DataFrame.loc[:, <subset>],在应用函数之前将data限制为。- **kwargs:dict
传递给
func。
- self:造型器
参数:
返回:
注意:
func的输出元素应该是 CSS 样式的字符串,格式为 ‘attribute:value;属性2:值2; ...' 或者,如果不对该元素应用任何内容,则为空字符串或None。这类似于
DataFrame.apply,除了axis=None一次将函数应用于整个 DataFrame,而不是按列或按行。例子:
>>> def highlight_max(x, color): ... return np.where(x == np.nanmax(x.to_numpy()), f"color:{color};", None) >>> df = pd.DataFrame(np.random.randn(5, 2), columns=["A", "B"]) >>> df.style.apply(highlight_max, color='red') >>> df.style.apply(highlight_max, color='blue', axis=1) >>> df.style.apply(highlight_max, color='green', axis=None)使用
subset将应用程序限制为单列或多列>>> df.style.apply(highlight_max, color='red', subset="A") ... >>> df.style.apply(highlight_max, color='red', subset=["A", "B"]) ...使用
subset的二维输入来选择除列之外的行>>> df.style.apply(highlight_max, color='red', subset=([0,1,2], slice(None))) ... >>> df.style.apply(highlight_max, color='red', subset=(slice(0,5,2), "A")) ...使用返回长度不等但包含有效索引标签的系列/数据帧的函数
>>> df = pd.DataFrame([[1, 2], [3, 4], [4, 6]], index=["A1", "A2", "Total"]) >>> total_style = pd.Series("font-weight:bold;", index=["Total"]) >>> df.style.apply(lambda s:total_style)有关更多详细信息,请参阅表可视化用户指南。
相关用法
- Python pandas.io.formats.style.Styler.apply_index用法及代码示例
- Python pandas.io.formats.style.Styler.applymap用法及代码示例
- Python pandas.io.formats.style.Styler.applymap_index用法及代码示例
- Python pandas.io.formats.style.Styler.format_index用法及代码示例
- Python pandas.io.formats.style.Styler.text_gradient用法及代码示例
- Python pandas.io.formats.style.Styler.hide用法及代码示例
- Python pandas.io.formats.style.Styler.set_table_attributes用法及代码示例
- Python pandas.io.formats.style.Styler.set_tooltips用法及代码示例
- Python pandas.io.formats.style.Styler.set_properties用法及代码示例
- Python pandas.io.formats.style.Styler.set_td_classes用法及代码示例
- Python pandas.io.formats.style.Styler.to_latex用法及代码示例
- Python pandas.io.formats.style.Styler.pipe用法及代码示例
- Python pandas.io.formats.style.Styler.where用法及代码示例
- Python pandas.io.formats.style.Styler.format用法及代码示例
- Python pandas.io.formats.style.Styler.highlight_between用法及代码示例
- Python pandas.io.formats.style.Styler.use用法及代码示例
- Python pandas.io.formats.style.Styler.background_gradient用法及代码示例
- Python pandas.io.formats.style.Styler.to_excel用法及代码示例
- Python pandas.io.formats.style.Styler.highlight_quantile用法及代码示例
- Python pandas.io.formats.style.Styler.export用法及代码示例
注:本文由纯净天空筛选整理自pandas.pydata.org大神的英文原创作品 pandas.io.formats.style.Styler.apply。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
