Home > database >  How to center Headers and Common Text of a Dataframe that will create an Image with these values?
How to center Headers and Common Text of a Dataframe that will create an Image with these values?

Time:02-21

If I want to center the headers to print, we can do:

pd.set_option('colheader_justify', 'center')

But when I try to center all the text in creating an image with the values, they keep positioned not centered:

DATA,HORA,CAMPEONATO,JOGO,CANAIS
20/02/2022,08:30,Campeonato Italiano,Fiorentina x Atalanta,"ESPN 2, STAR "
20/02/2022,09:00,Campeonato Escocês,Dundee Utd x Rangers,STAR 
20/02/2022,09:00,Campeonato Piauiense,Flamengo PI x Fluminense PI,ELEVENSPORTS.COM

import pandas as pd
import dataframe_image as dfi

df = pd.read_csv("tabela_para_imagem.csv")
pd.set_option('colheader_justify', 'center')
df_styled = df.style.set_properties(**{'text-align': 'center'})
dfi.export(df_styled,"mytable.png")

enter image description here

My expected result is:

enter image description here

How should I proceed to be able to centralize all the values?

CodePudding user response:

You just need to do this:

import pandas as pd
import dataframe_image as dfi

df = pd.read_csv("file.csv")

#df_styled = df.style.background_gradient()
pd.set_option('colheader_justify', 'center')

dfi.export(df,"mytable.png")

which gives:

enter image description here

To center also the values:

import pandas as pd
import dataframe_image as dfi

df = pd.read_csv("file.csv")

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

dfi.export(df,"mytable.png")

which gives

enter image description here

  • Related