我有一个 pandas dataFrame,我正在使用 to_html()
将其转换为 HTML 表格,但是我想根据返回的 HTML 表格中的值为某些单元格着色。
知道怎么做吗?
例如:名为“abc”的列中值大于 5 的所有单元格必须显示为红色,否则为蓝色。
最佳答案
这是一种方法:
df = pd.DataFrame(np.random.randint(0,10, (5,3)), columns=list('abc'))
def color_cell(cell):
return 'color: ' + ('red' if cell > 5 else 'green')
html = df.style.applymap(color_cell, subset=['a']).render()
with open('c:/temp/a.html', 'w') as f:
f.write(html)
结果:
关于python - 我如何根据使用 to_html 从 pd.DataFrame 生成的 HTML 表的值更改颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38849992/