pandas style - how to hide the column labels?

Viewed 2041

How do I hide the column labels via pandas style? There is a hide_index() method that removes the index row, unfortunately the hide_column() label removes the entire column (both the header and the data). I just want to hide the headers. thanks!

2 Answers

set_table_styles

You can set the style for the table. Docs

Setup

df = pd.DataFrame(1, range(3), ['Look', 'At', 'My', 'Header'])

df.style

enter image description here

df.style.set_table_styles([
    {'selector': 'thead', 'props': [('display', 'none')]}
])

enter image description here

As part of the Styler Enhancements in pandas 1.3.0 hide_columns() with no subset will simply hide the columns from display, no longer removing the associated column data.

import pandas as pd

df = pd.DataFrame(1, range(3), ['Look', 'At', 'My', 'Header'])
df.style.hide_columns()

styled table with no header (new hide_columns behaviour)


In pandas 1.4.0 hide_columns was deprecated (GH43758) in favour of hide with axis=..., but keeps the same behaviour as 1.3.0's hide_columns:

import pandas as pd

df = pd.DataFrame(1, range(3), ['Look', 'At', 'My', 'Header'])
df.style.hide(axis='columns')

styled table with no header (using hide(axis='columns'))

Related