Opt*_*rew 6 python conditional-formatting styling pandas
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!
jez*_*ael 13
您可以DataFrame通过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)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5372 次 |
| 最近记录: |