I am quite new to the Pandas styling
and trying to style my Pandas DataFrame
and colour
the headers
grey colour
, here is my attempt:
with open ('test.html','w') as test:
test, df.style.set_properties(**{'text-align': 'center'}).set_table_styles([ dict(selector='th', props=[('text-align', 'center')] ) ]).render()
There are 2 problems and 1 question:
Problems
1 - To colour the headers grey
like the excel
2 - exporting as HTML
Question: Is it possible to render the finished styled as an excel file as well?
Yes, you can do:
def hover(hover_color="#ffff99"):
return dict(selector="tr:hover",
props=[("background-color", "%s" % hover_color)])
styles = [
hover(),
dict(selector='thead', props=[('background-color','grey')]),
]
# this forces grey background on header
styler = df.style.set_table_styles(styles)
# HTML string
html = styler.render()
# save html
with open('file.html', 'w') as f:
f.write(html)
# excel
styler.to_excel('file.xlsx')