Zouhair
Zouhair

Reputation: 43

DataFrame display not as expected

I tried to color the title of the columns in purple, but what I got from the output doesn't seem aligned. Is there a way to fix it?

import pandas as pd

purple_text = '\033[35m'
reset = '\033[m'

list_1 = [12, 27, 33, 56, 11, 90]
list_2 = [43, 55, 76, 26, 84, 62]


df = pd.DataFrame({f'{purple_text} Numbers_1 {reset}': list_1,
                   f'{purple_text} Numbers_2 {reset}': list_2})

print(df.to_string(index=False))

This is the output

Upvotes: 2

Views: 113

Answers (1)

Mathis Germa
Mathis Germa

Reputation: 113

Your issue comes from the fact that this formatting is making header text an incorrect size.

In order to remedy this, you should use display settings, this one works fine:

pd.set_option('display.colheader_justify', 'left')

Results: Aligned

Upvotes: 3

Related Questions