How to remove header from pandas Styler?

Viewed 228

I want to remove the header from the pandas Styler so that I can render it. What I have tried:

def highlight(x):
    c1 = 'background-color: #f5f5dc'
    df1 = pd.DataFrame('', index=x.index, columns=x.columns)
    df1.loc[['A'], :] = c1
    return df1
temp = {'col1': ['abc', 'def'], 'col2': [1.0, 2.0]}
df = pd.DataFrame(temp)
df.index = ['A', 'B']
print(df)
df.style.apply(highlight, axis=None).hide_index()

Output

col1  col2
-------------
abc   1.000000
def   2.000000

But I want to remove col1 and col2 as it comes in my post rendering page which I don't need.

Is there any way I can do it?

1 Answers

In pandas 1.4.0 both hide_index and hide_columns were deprecated (GH43758) in favour of hide with axis=...

With labels:

df.style.apply(highlight, axis=None).hide(axis='index').hide(axis='columns')

With axis numbers:

df.style.apply(highlight, axis=None).hide(axis=0).hide(axis=1)

styled table with no index or columns


In versions prior to 1.4.0 the hide_columns function can be used to hide the columns. Similar to how the hide_index function does for the index:

df.style.apply(highlight, axis=None).hide_index().hide_columns()

styled table with no index or columns

Related