我有一个像DataFrame
df = pd.DataFrame(np.random.randn(10).reshape(2, 5))
df
# 0 1 2 3 4
# 0 -0.067162 -0.505401 -0.019208 1.123936 0.087682
# 1 -0.373212 -0.598412 0.185211 0.736143 -0.469111
我试图将此DataFrame输出为HTML,之前使用的是to_html
df.to_html(classes=['table', 'table-hover', 'table-bordered'],
float_format=lambda x: '{0:.3f}s'.format(x))
但后来我遇到了Style功能,并认为在我的DataFrame中为浮动设备设置样式会更好.喜欢
def colorize(num)
color = 'red' if (np.isnan(num) or num > 0) else 'green'
return 'color: %s' % color
我可以将其应用于我的DataFrame
df_styler = df.Style.applymap(colorize)
但是现在df_styler是一个Styler对象,虽然它有一个render方法,但我看不出我怎样才能传递我使用to_html的类列表或float格式化程序…
有没有办法可以使用Style函数和to_html中的CSS类/格式化程序组合?