Python:用Pandas输出格式化HTML并高亮
2022-05-19 10:14
1526 查看
输出格式化的HTML
我们已知一个DataFrame记录了模型Model1、Model2在3个Epoch优化中的精度变化情况:
frame = pd.DataFrame({"Model1": [0.620, 0.832, 0.902], "Model2": [0.683, 0.867, 0.835]}, index=\ ["Epoch {}".format(r + 1) for r in range(3)]). print(frame) # Model1 Mode2 # Epoch 1 0.620 0.683 # Epoch 2 0.832 0.867 # Epoch 3 0.902 0.835
现在我们像将其输出为html,我们可以这么做:
frame.to_html("Pandas/cluster_log.html")
浏览器打开效果如下:
我们发现这样并不美观。我们想要去除掉表格的边框,并使每个单元格对齐,可以为表格设置
style属性再输出:
frame = frame.style.set_properties(**{'background-color': 'white', "align":"center"}) ## **表示以关键字参数传参,等效于background-color=white形式 frame.to_html("Pandas/cluster_log.html")
增加高亮效果
如果我们想对各模型迭代过程中取得的最大精度进行高亮,可以调用
apply()方法并传入一个回调函数:
def highlight_max(series): #遍历frame的每一列(series)依次设置效果 is_max = series == series.max() return ['background-color: yellow' if value else '' for value in is_max] frame = frame.style.apply(highlight_max) frame.to_html("Pandas/highlight_cluster_log.html")
浏览器打开效果如下:
相关文章推荐
- Python笔记,Day1 —— print高亮标记方式、头文件中env含义、格式化输出、循环
- python编程:格式化输出
- python 笔记 继续进行输出练习,并进行格式化定义打印 ——12.23
- python 测试报告输出 htmltestrunner 及 中文乱码的解决方式
- Python格式化输出
- python输出当前目录下index.html文件路径的方法
- Python学习-格式化输出
- python格式化输出
- Python3.2官方文档翻译--输出格式化
- python输出htmltestrunner中文乱码如何解决
- 解决Python pandas plot输出图形中显示中文乱码问题
- python格式化输出
- python - 用户交互/数据类型/格式化输出/运算符/流程控制单双多分支
- Python 以指定宽度格式化输出(format)
- Python3 格式化输出
- Python格式化输出(%操作符)
- python 浅析格式化输出和深浅copy
- Python格式化输出
- Python格式化输出之format函数
- python3中输出高亮