我正在尝试使用烧瓶显示熊猫数据框。我成功地这样做了,直到我决定为一些数据框的行着色。特别是当我应用to_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 highlight_greaterthan(s,threshold,column):
is_max = pd.Series(data=False, index=s.index)
is_max[column] = s.loc[column] >= threshold
return ['background-color: yellow' if is_max.any() else '' for v in is_max]
df = df.style.apply(highlight_greaterthan,threshold=1.0,column=['C','B'], axis=1)
display(df)
接下来,当我运行to_html()
它时,一切都崩溃了。
df_html = df.to_html
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-28-4d0cc094240b> in <module>()
----> 1 df_html = df.to_html
AttributeError: 'Styler' object has no attribute 'to_html'
关于如何保留行颜色的任何想法?谢谢!