How to change pandas display font of index column

Viewed 227
data = {
    'X': [3, 2, 0, 1], 
    'Y': [0, 3, 7, 2]
}

df = pd.DataFrame(data, index=['A', 'B', 'C', 'D'])
df.style.set_properties(**{
    'font-family':'Courier New'
})
df

The index column is displayed in bold, is it possible to change font of index column?

1 Answers

You must use table_styles. In this example I manage to make the "font-weight":"normal" for the index and columns:

Let's define some test data:

import pandas as pd
df = pd.DataFrame({'A':[1,2,3,4],
                   'B':[5,4,3,1]})

We define style customization to use:

styles = [
    dict(selector="th", props=[("font-weight","normal"),
                               ("text-align", "center")])]

We pass the style variable as the argument for set_table_styles():

html = (df.style.set_table_styles(styles))
html

And the output is: enter image description here

Please feel free to read about the documentation in pandas Styling for more details.

Related