Center align all dataframe headers except two that one of them must align left and the other right

Question:

The code below creates a table with the values of all columns centered and the column titles also centered. I align the values in the local_team column to the right and the values in the visitor_team column to the left:

ef dfi_image(list_dict,name_file):
    df = pd.DataFrame(list_dict)
    df = df[['time','competition','local_team','score','visitor_team','channels']]

    df = df.style.set_table_styles([dict(selector='th', props=[('text-align', 'center'),('background-color', '#40466e'),('color', 'white')])])
    df.set_properties(**{'text-align': 'center'}).hide(axis='index')
    df.set_properties(subset=['local_team'], **{'text-align': 'right'}).hide(axis='index')
    df.set_properties(subset=['visitor_team'], **{'text-align': 'left'}).hide(axis='index')
    
    dfi.export(df, name_file + ".png")
dfi_image(games,'table_dfi.png')

enter image description here

But the title of local_team doesn’t align to the right and neither the title of visitor_team aligns to the left and I need them with equal alignment to the values of these columns.

So i try:

def dfi_image(list_dict,name_file):
    df = pd.DataFrame(list_dict)
    df = df[['time','competition','local_team','score','visitor_team','channels']]

    df = df.style.set_table_styles([
        dict(selector='th', props=[('text-align', 'center'), ('background-color', '#40466e'), ('color', 'white')]),
        dict(selector='th.col_heading.local_team', props=[('text-align', 'right')]),
        dict(selector='th.col_heading.visitor_team', props=[('text-align', 'left')])
    ])
    df.set_properties(**{'text-align': 'center'}).hide(axis='index')
    df.set_properties(subset=['local_team'], **{'text-align': 'right'}).hide(axis='index')
    df.set_properties(subset=['visitor_team'], **{'text-align': 'left'}).hide(axis='index')

    dfi.export(df, name_file + ".png")
dfi_image(games,'table_dfi.png')

enter image description here

how to proceed for the result is equal this image:

enter image description here

Asked By: Digital Farmer

||

Answers:

Here is one way to do it with Pandas Styler.set_table_styles:

import pandas as pd


df = pd.DataFrame(
    {
        "col1": [1000000000, 2000000000],
        "col2": [4000000000, 5000000000],
        "col3": [7000000000, 8000000000],
        "col4": [10000000000, 11000000000],
        "col5": [13000000000, 14000000000],
    }
)

To center align all column headers except col2 (right align) and col3 (left align):

styles = (
    {
        key: [{"selector": "th", "props": [("text-align", "center")]}]
        for key in df.columns
        if key not in ["col2", "col3"]
    }
    | {"col2": [{"selector": "th", "props": [("text-align", "right")]}]}
    | {"col3": [{"selector": "th", "props": [("text-align", "left")]}]}
)

df.style.set_table_styles(styles)

Output in a Jupyter notebook cell:

enter image description here

Answered By: Laurent
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.