How to center align headers and values in a dataframe, and how to drop the index in a dataframe

Issue

I have the following dataframe:

import pandas as pd

df = pd.DataFrame({'text': ['foo foo', 'bar bar'],
                 'number': [1, 2]})
df

dataframe

How do I center-align both the column titles/headers and the values in a dataframe, and how do I drop the index (the column with the values 0 and 1) in a dataframe?

Solution

Found an answer for this. This should do the trick to center-align both headers and values and hiding the index:

df1 = df.style.set_table_styles([dict(selector='th', props=[('text-align', 'center')])])
df1.set_properties(**{'text-align': 'center'}).hide_index()

Answered By – Leockl

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published