【发布时间】:2021-01-28 22:02:38
【问题描述】:
我正在尝试在电子邮件正文中向最终用户显示多个数据框。有 100 个最终用户,每个 df 都经过过滤以仅显示他们的条目。某些数据框可能为空,具体取决于最终用户创建的记录。我的代码如下所示:
email_body = """\
<html>
<head></head>
<body>
<br>{Coa_df}<br>
<br>{IA_df}<br>
<br>{Part_df}<br>
<br>{PA_df}<br>
<br>{PSE_df}<br>
</body>
</html>
"""
new_email_body = email_body.format(Coa_df = '<h1> Coalitions </h1>' + Coa_df.to_html(),
IA_df = '<h1> Indirect Activities </h1>' + IA_df.to_html(),
Part_df = '<h1> Partnerships </h1>' + Part_df.to_html(),
PA_df = '<h1> Program Activities </h1>' + PA_df.to_html(),
PSE_df = '<h1> PSE Site Activities </h1>' + PSE_df.to_html())
但是,这将显示每个数据帧,无论它们是否为空。
为了专门显示非空数据帧,我创建了一个 dfs 字典来运行 for 循环。但是,我不确定如何以这种方式显示多个数据框。这是我得到的:
email_body = """\
<html>
<head></head>
<body>
<br>{df1}<br>
</body>
</html>
"""
dfs = {'Coalitions' : Coa_df, 'Indirect Activities' : IA_df, 'Partnerships' : Part_df, 'Program Activities' : PA_df, 'PSE Site Activities' : PSE_df}
for heading, df in dfs.items():
if df.empty == False:
new_email_body = email_body.(staff_name = staff_name, deadline_date = deadline_date, df1 = '<h1> ' + heading + ' </h1>' + df.to_html())
这只会显示 html 中最后一个非空的 df。我尝试显示每个非空 df 都会导致错误。
【问题讨论】: