猿问

使用突出显示的单元格渲染 pandas dataframe ot html

我有一个 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 name

text_file = open(file_name, "w")

text_file.write("df Parameters \n" + html)

text_file.close()

数据帧包含浮点数和布尔值的组合。目标是使用一组条件渲染数据帧。在此特定数据框中,如果小于 10,浮点数将突出显示为绿色,如果大于 10,则将突出显示红色。此外,包含 True 的单元格将显示为绿色,包含 False 的单元格将显示为红色:

守着一只汪
浏览 97回答 1
1回答

一只名叫tom的猫

一种解决方案是使用 pandas 样式,如下所示:import pandas as pddf = pd.DataFrame()df['parm A'] = [9.5, 8.3, 13]df['parm B'] = [True, False, True]def red_green(s):&nbsp; &nbsp; 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 nametext_file = open(file_name, "w")text_file.write("df Parameters \n" + html)text_file.close()&nbsp;
随时随地看视频慕课网APP

相关分类

Html5
我要回答