13

I have formatted a pandas dataframe using .style, and would like to send out the formatted table as an email. However, styler objects are not compatible with the to_html function, instead I then tried to use the .render() function.

However, .render() seems to remove quite a lot of the formatting e.g. the table border disappears and some text becomes centred. I'm trying to avoid editing the html string produced by .render() but I understand that may not be possible.

What are my other options for sending out a formatted table as an email?

Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
dwdwd
  • 131
  • 1
  • 1
  • 4

5 Answers5

13

I had the same problem. so after rendering the Style object I created a text file and write the Style object into it. After exporting the text file, change the format to html and you should be fine.

f=open("styled_dataframe.txt","w")
f.write(df.render()) # df is the styled dataframe
f.close()
osmancakirio
  • 507
  • 1
  • 9
  • 20
  • 6
    I wrote the file as an .html and it worked. So you can avoid writing the file as a .txt and then converting it later. – Dan Fiorino Oct 12 '18 at 17:26
2

I too was trying to format a dataframe something like the to_html function but with changes to font and color. Most of the Google found examples use the built in pandas styler functions for their examples, but I needed something simpler. The code that I ended up with was:

df = pd.DataFrame(np.random.randn(15).reshape(5,3))

print(df)
df_styled = df.style.format('{:,.2f}').set_properties(**{
    'font': 'Arial', 'color': 'red', 'text-align': 'center'})
df_styled.set_table_styles([{'props': [('background-color', 'MintCream'), ('color', 'blue'), ('font', 'Arial'), ('font-weight', 'bold')]}])
df_styled.set_table_attributes('border="1"')

with open('test.html', 'w') as f:
    f.write(df_styled.render())

Since I am relatively new to python and pandas I originally thought that the styler was added to the original dataframe. I now understand that the df.style..... returns a styled dataframe object which can handle further property and attribute changes before finally being rendered. In this sample I rendered the styled dataframe to a file which can then be opened with a browser to see the result. In my actual application I rendered the styled dataframe to a string object which was then sent in and email as a html mime type as the OP originally wanted.

ChrisH
  • 71
  • 2
0

You need to set the overwrite argument to False to apply all the changes to the sytle. I think it is better if you pipe the changes.

df_styled = df.style.format('{:,.2f}').set_properties(**{
    'font': 'Arial', 'color': 'red', 'text-align': 'center'},
    overwrite=False,
).set_table_styles(
    [{'props': [('background-color', 'MintCream'), ('color', 'blue'), 
    ('font', 'Arial'), ('font-weight', 'bold')]}],    
    overwrite=False,
).set_table_attributes('border="1"',overwrite=False,)
0

The styled dataframe cannot be exported with the option render() as it's no longer supported.

Pandas 1.3 has an style.to_html() method this should be used instead. For more information read the documentation under: pandas.io.formats.style.Styler.to_html

eg:

f=open("Data.html","w") 
f.write(s1.to_html()) # s1 is the stylized df
f.close()
Badri
  • 67
  • 1
  • 8
0

Sharing what worked for me:

df.style.bar(subset=['column_x']).to_html('yourHtmlFileName.html') 
HassanSh__3571619
  • 1,859
  • 1
  • 19
  • 18