When I print the dataframe in python, it is well formatted and centre aligned. However, when I send the dataframe in an email, the data and column headers are all over the place.
df1 = pd.DataFrame(data=data1, columns=cols1)
print(df1)
Output
Race Horse Odds W%
0 Ascot R1 Cliffs Of Comfort 2.5 40.000000
1 Ascot R1 Tommy Blue 2.8 35.714286
2 Ascot R1 Semigel 12.2 8.196721
3 Ascot R1 River Beau 13.0 7.692308
4 Ascot R1 Rocky Path 21.0 4.761905
5 Ascot R1 Melody's Secret 41.0 2.439024
6 Ascot R1 Art Admirer 201.0 0.497512
7 Ascot R1 Lies 'N' Deception 151.0 0.662252
8 Ascot R1 Express Time 11.0 9.090909
9 Ascot R1 Single Finger 201.0 0.497512
Code for sending email
for data in df1, df2:
datetime = datetime.now()
subject = f'Market Efficiency Update - {datetime}'
body = f'Hi Harrison,\n\nMarket efficiency is:{df2} \n\n{df1}'
message = f'Subject: {subject}\n\n{body}'
mail = smtplib.SMTP('smtp.gmail.com', 587)
I think pandas
function DataFrame.to_string()
would help you here:
for data in df1, df2:
(...)
df1_str = df1.to_string()
body = f'Hi Harrison,\n\nMarket efficiency is:{df2} \n\n{df1_str}'
(...)
Try converting DataFrame to HTML format as below:
for data in df1, df2:
datetime = datetime.now()
subject = f'Market Efficiency Update - {datetime}'
body = f'Hi Harrison,\n\nMarket efficiency is:{df2.to_html()} \n\n{df1.to_html()}'
message = f'Subject: {subject}\n\n{body}'
mail = smtplib.SMTP('smtp.gmail.com', 587)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.