Search code examples
pythonpandasdataframealignment

Pandas dataframe: alignment


I'm trying to give my custom style to my dataframe visualization. Here is my example code:

import pandas as pd
import numpy as np

np.random.seed(24)
df = pd.DataFrame({'A': np.linspace(1, 10, 10)})
df = pd.concat([df, pd.DataFrame(np.random.randn(10, 4), columns=list('BCDE'))],axis=1)
df.iloc[0, 2] = np.nan
df.style.bar(subset=['A', 'B'], color='#d65f5f').hide_index().set_properties(**{'text-align': 'left'})

my problem is that column names are aligned to the right. I couldn't find a way to correct it. help me with this, please!


Solution

  • You can use these when printing:

     print(df.to_string(justify='center', index=False))  
     print(df.to_string(justify='left', index=False))