I'm trying to create a dataframe that incorporates a rule where cells are highlighted depending on their value. I've done this using pandas.style (code for a simplified dataframe below).
y = pd.DataFrame({'A':[2,4,6,1,4,7],
'B':[9,5,3,6,2,5],
'C':[0,6,3,5,6,2]})
index_max = pd.Series([2,5,2,7,3,4])
column_max = pd.Series([1,5,7], index=['A', 'B', 'C'])
def highlight_(s):
to_highlight = (s > index_max) & (s > column_max.loc[s.name])
return ['background-color: yellow' if v else '' for v in to_highlight]
y.style.apply(highlight_)
This works but the dataframe I'm using is quite large and has multi-indexing, so I wanted to make it more presentable. I found the below CSS snippet that uses the magic function to apply a format that I liked (source: https://medium.com/analytics-vidhya/5-css-snippets-for-better-looking-pandas-dataframes-ad808e407894)
%%HTML
<style>.dataframe th{
background: rgb(255,255,255);
background: radial-gradient(circle, rgba(255,255,255,1) 0%, rgba(236,236,236,1) 100%);
padding: 5px;
color: #343434;
font-family: monospace;
font-size: 110%;
border:2px solid #e0e0e0;
text-align:left !important;
}.dataframe{border: 3px solid #ffebeb !important;}</style>
The problem is that I can't find a way to combine the two. I either get a style object with just the highlighting rule applied or the original dataframe with the nice new html formatting but that doesn't apply the highlighting rule. I'm hoping there's a way to combine html and the styler to achieve an output with BOTH, but I don't really know where to start.
Any help would be massively appreciated!