pandas.io.formats.style.Styler.hide_columns

Styler.hide_columns(subset=None, level=None, names=False)[source]

Hide the column headers or specific keys in the columns from rendering.

This method has dual functionality:

  • if subset is None then the entire column headers row, or specific levels, will be hidden whilst the data-values remain visible.

  • if a subset is given then those specific columns, including the data-values will be hidden, whilst the column headers row remains visible.

Changed in version 1.3.0.

Parameters
subsetlabel, array-like, IndexSlice, optional

A valid 1d input or single key along the columns axis within DataFrame.loc[:, <subset>], to limit data to before applying the function.

levelint, str, list

The level(s) to hide in a MultiIndex if hiding the entire column headers row. Cannot be used simultaneously with subset.

New in version 1.4.0.

namesbool

Whether to hide the column index name(s), in the case all column headers, or some levels, are visible.

New in version 1.4.0.

Returns
selfStyler

See also

Styler.hide_index

Hide the entire index, or specific keys in the index.

Examples

Simple application hiding specific columns:

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

Hide column headers and retain the data values:

>>> 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_columns()  
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

Hide specific columns but retain the column headers:

>>> df.style.format("{:.1f}").hide_columns(subset=(slice(None), ["a", "c"]))
...   
           x      y
           b      b
x   a    0.0    0.6
    b    1.0   -0.0
    c   -0.8   -0.4
y   a    1.0   -1.2
    b    1.2    0.3
    c    0.5    2.2

Hide specific columns and the column headers:

>>> df.style.format("{:.1f}").hide_columns(
...    subset=(slice(None), ["a", "c"])).hide_columns()  
x   a    0.0    0.6
    b    1.0   -0.0
    c   -0.8   -0.4
y   a    1.0   -1.2
    b    1.2    0.3
    c    0.5    2.2

Hide a specific level:

>>> df.style.format("{:.1f}").hide_columns(level=1)  
           x                    y
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

Hiding just the column level names:

>>> df.columns.names = ["lev0", "lev1"]
>>> df.style.format("{:.1f").hide_columns(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