我想将pandas DataFrame输出到.html并应用样式.文档*提供了此示例,该示例将负值显示为红色.
import pandas as pd
import numpy as np
np.random.seed(24)
df = pd.DataFrame({'A': np.linspace(1, 10, 10)})
df = pd.concat([df, pd.DataFrame(np.random.randn(10, 4), columns=list('BCDE'))],
axis=1)
df.iloc[0, 2] = np.nan
def color_negative_red(val):
"""
Takes a scalar and returns a string with
the css property `'color: red'` for negative
strings, black otherwise.
"""
color = 'red' if val < 0 else 'black'
return 'color: %s' % color
s = df.style.applymap(color_negative_red)
s
我可以使用“ to_html”方法输出格式化的df吗?从该文档中**,我看到有一个“格式化程序”选项,但在这种情况下我不知道如何应用.
这是我编写无格式df的方法:
df.to_html(r'c:\temp\html.html')
本文介绍如何使用 Pandas 将 DataFrame 输出为 HTML 并应用样式,具体演示了如何将负值显示为红色,同时提供了实现这一功能的 Python 代码。
1万+

被折叠的 条评论
为什么被折叠?



