我有一个熊猫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 name
text_file = open(file_name, "w")
text_file.write("df Parameters \n" + html)
text_file.close()
数据框包含浮点数和布尔值的组合。目标是使用一组条件渲染数据框。在此特定数据框中,如果浮点数小于10,则浮标将突出显示为绿色,如果大于10,浮点将被突出显示为红色。此外,包含True的单元格将变为绿色,而False的单元格将变为红色:
答案 0 :(得分:1)
一种解决方案是使用熊猫样式,这是这样:
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()