[英]How to make dataframe content in e-mail go red in html based off results using Python
I am e-mailing a dataframe in the body of the e-mail.我正在电子邮件正文中发送 dataframe。 However, I want to format the dataframe results so that if my results are a specific value (non-0 value) then the text turns red.但是,我想格式化 dataframe 结果,以便如果我的结果是特定值(非 0 值),则文本变为红色。
Please see below for my function which I turn into a dataframe to be sent in an e-mail请在下面查看我的 function,我将其转换为 dataframe 以通过电子邮件发送
def id_check():
df = pd.read_csv('filename.csv', low_memory=False)
missing_id = df["id"].isna().sum()
print("Number of records missing an id:", missing_id)
return missing_id
id_table = {
'Check' : ['ID Check'],
'Summary' : ['Number of records missing an ID'],
'Findings' : [id_check()]
}
df_id = pd.DataFrame.from_dict(id_table)
df_id.head()
the result looks like结果看起来像
Check Summary Findings
0 ID Check Number of records missing an ID 0
However, if the findings ever become,= 0. I'd like the findings result to turn red?但是,如果调查结果变为,= 0。我希望调查结果变为红色? Is there a way to do that?有没有办法做到这一点? Currently i am just outputting my results and by default it is black目前我只是输出我的结果,默认情况下它是黑色的
See below for my html e-mail code.请参阅下面我的 html 电子邮件代码。
msg = MIMEMultipart('mixed')
msg['Subject'] = SUBJECT
msg['From'] = FROM
msg['To'] = ','.join(TO)
html = f"""
<html><body>
<h1> </h1>
<p> Please see the the summary below {df_id.to_html(index=False)} </p>
</body></html>
"""
msg_text = MIMEText(html, 'html')
msg.attach(msg_text)
Use lambda function with Styler.apply
for coloring red row if no 0
in Findings
column, Styler.hide
is for hide index, for html is used Styler.to_html
:如果Findings
列中没有0
,则使用 lambda function 和Styler.apply
为红色行着色, Styler.hide
用于隐藏索引,html 使用Styler.to_html
:
color= lambda x: (pd.DataFrame('', index=x.index, columns=x.columns)
.mask(x['Findings'].ne(0), 'color:red;'))
html = df_id.style.apply(color, axis=None).hide().to_html()
#for oldier pandas version use
#html = df_id.style.apply(color, axis=None).hide_index().render()
html = f"""
<html><body>
<h1> </h1>
<p> Please see the the summary below {html} </p>
</body></html>
"""
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.