I've searched and can't seem to find an answer on this anywhere, so hopefully it's possible. I have a dataframe, for simplicity I'll include an abbreviated version below. What I'd like to do is apply a custom formula for styling, or style one particular column based on the values in another column.
Using this as an example, I'd like to highlight the Current column's cells where the Diff > Historic Standard Dev in that row.
I've explored the style.apply approaches, but can't seem to find one that works. Any help would be greatly appreciated.
Thanks!
You can create DataFrame
of styles by Styler.apply
:
def select_col(x):
c1 = 'background-color: red'
c2 = ''
#compare columns
mask = x['Diff'] > x['HistoricStandardDev']
#DataFrame with same index and columns names as original filled empty strings
df1 = pd.DataFrame(c2, index=x.index, columns=x.columns)
#modify values of df1 column by boolean mask
df1.loc[mask, 'Current'] = c1
return df1
df.style.apply(select_col, axis=None)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With