我有一个 pandas daraframe,我正在将其渲染为 html:df = pd.DataFrame()df['parm A'] = [9.5, 8.2, 13]df['parm B'] = [True, False, True]html = df.to_html()path = "C:\\path"file_name = "file.html" #make file name specific to patient and plan nametext_file = open(file_name, "w")text_file.write("df Parameters \n" + html)text_file.close()数据帧包含浮点数和布尔值的组合。目标是使用一组条件渲染数据帧。在此特定数据框中,如果小于 10,浮点数将突出显示为绿色,如果大于 10,则将突出显示红色。此外,包含 True 的单元格将显示为绿色,包含 False 的单元格将显示为红色:
1 回答
一只名叫tom的猫
TA贡献1906条经验 获得超3个赞
一种解决方案是使用 pandas 样式,如下所示:
import pandas as pd
df = pd.DataFrame()
df['parm A'] = [9.5, 8.3, 13]
df['parm B'] = [True, False, True]
def red_green(s):
return ['background-color: green' if (v<10 and v) else 'background-color: red' for v in s]
html = df.style.apply(red_green).render()
path = "C:\\path"
file_name = "file.html" #make file name specific to patient and plan name
text_file = open(file_name, "w")
text_file.write("df Parameters \n" + html)
text_file.close()
- 1 回答
- 0 关注
- 96 浏览
添加回答
举报
0/150
提交
取消