pandas.io.formats.style.Styler.hide#

Styler.hide(subset=None, axis=0, level=None, names=False)[源代码]#

隐藏整个索引/列标题,或从显示中隐藏特定行/列。

在 1.4.0 版本加入.

Parameters:
subset标签、类数组、IndexSlice、可选

DataFrame.loc[<subset>, :]DataFrame.loc[:, <subset>] 中沿轴的有效一维输入或单个键,具体取决于 axis,用于限制 data 以选择隐藏的行/列。

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

应用于索引或列。

levelint, str, list

在隐藏整个索引/列标题的情况下,要隐藏 MultiIndex 中的级别。不能与 subset 同时使用。

namesbool

在索引/列标题的级别名称(如果它(或至少其中一个级别)仍然可见)的情况下,是否隐藏级别名称。

Returns:
Styler

Notes

警告

此方法仅适用于输出方法 to_htmlto_stringto_latex

其他输出方法,包括 to_excel,将忽略此隐藏方法并显示所有数据。

此方法根据 subsetlevelnames 参数的组合实现多种功能(请参阅示例)。axis 参数仅用于控制该方法应用于行还是列标题:

参数组合#

subset

level

names

效果

None

None

False

轴索引将完全隐藏。

None

None

True

仅隐藏轴索引名称。

None

Int, Str, List

False

指定的轴 MultiIndex 级别将完全隐藏。

None

Int, Str, List

True

指定的轴 MultiIndex 级别将完全隐藏,以及剩余轴 MultiIndex 级别的名称。

Subset

None

False

指定的行/列数据将被隐藏,但轴索引本身和名称保持不变。

Subset

None

True

指定的行/列数据和轴索引名称被隐藏,但轴索引本身保持不变。

Subset

Int, Str, List

Boolean

ValueError: 不能同时提供 subsetlevel

请注意,此方法仅隐藏已标识的元素,因此可以进行链式调用以按顺序隐藏多个元素。

Examples

简单地隐藏特定行的应用:

>>> df = pd.DataFrame([[1,2], [3,4], [5,6]], index=["a", "b", "c"])
>>> df.style.hide(["a", "b"])  
     0    1
c    5    6

隐藏索引并保留数据值:

>>> midx = pd.MultiIndex.from_product([["x", "y"], ["a", "b", "c"]])
>>> df = pd.DataFrame(np.random.randn(6,6), index=midx, columns=midx)
>>> df.style.format("{:.1f}").hide()  
                 x                    y
   a      b      c      a      b      c
 0.1    0.0    0.4    1.3    0.6   -1.4
 0.7    1.0    1.3    1.5   -0.0   -0.2
 1.4   -0.8    1.6   -0.2   -0.4   -0.3
 0.4    1.0   -0.2   -0.8   -1.2    1.1
-0.6    1.2    1.8    1.9    0.3    0.3
 0.8    0.5   -0.3    1.2    2.2   -0.8

在 MultiIndex 中隐藏特定行但保留索引:

>>> df.style.format("{:.1f}").hide(subset=(slice(None), ["a", "c"]))
...   
                         x                    y
           a      b      c      a      b      c
x   b    0.7    1.0    1.3    1.5   -0.0   -0.2
y   b   -0.6    1.2    1.8    1.9    0.3    0.3

通过链式调用隐藏特定行和索引:

>>> df.style.format("{:.1f}").hide(subset=(slice(None), ["a", "c"])).hide()
...   
                 x                    y
   a      b      c      a      b      c
 0.7    1.0    1.3    1.5   -0.0   -0.2
-0.6    1.2    1.8    1.9    0.3    0.3

隐藏特定级别:

>>> df.style.format("{:,.1f}").hide(level=1)  
                     x                    y
       a      b      c      a      b      c
x    0.1    0.0    0.4    1.3    0.6   -1.4
     0.7    1.0    1.3    1.5   -0.0   -0.2
     1.4   -0.8    1.6   -0.2   -0.4   -0.3
y    0.4    1.0   -0.2   -0.8   -1.2    1.1
    -0.6    1.2    1.8    1.9    0.3    0.3
     0.8    0.5   -0.3    1.2    2.2   -0.8

仅隐藏索引级别的名称:

>>> df.index.names = ["lev0", "lev1"]
>>> df.style.format("{:,.1f}").hide(names=True)  
                         x                    y
           a      b      c      a      b      c
x   a    0.1    0.0    0.4    1.3    0.6   -1.4
    b    0.7    1.0    1.3    1.5   -0.0   -0.2
    c    1.4   -0.8    1.6   -0.2   -0.4   -0.3
y   a    0.4    1.0   -0.2   -0.8   -1.2    1.1
    b   -0.6    1.2    1.8    1.9    0.3    0.3
    c    0.8    0.5   -0.3    1.2    2.2   -0.8

所有示例都通过 axis="columns" 产生等效的转置效果。