I am trying to show a pandas dataframe using flask. I was successfully doing so, until I decided to colour some of the dataframe's rows. In particular I fail when I apply the to_html()
method of pandas.
The following code gets a table with some rows colored in yellow:
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)
Next, when I run to_html()
it all crashes.
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'
Any ideas on how I can preserve the color of the rows? thanks!