熊猫使用条件设置单元格颜色(在多索引中)



嗨,我想为特定元素设置颜色并在熊猫数据帧中编写 excel/html。

我的数据框是:

old                new            diff
query result       query result   result
1 q1    a1           q1    a1       True
2 q2    a2           q2    a5       False
3 q3    a3           q3    a3       True
4 q4    a4           q4    a6       False

我想在将此数据帧写入 excel/html 时突出显示['diff']['result']列中的"False"数据。

如何突出显示单元格?

谢谢

更改函数highlight_max,请使用Styler.applymap,对于选择MultiIndex列,请使用元组:

def coloring(val):
color = 'red' if val is False else ''
return 'color: %s' % color
df.style.applymap(coloring, subset=[('diff','result')])

这是你可以做的。

def color_false_red(val):
color = 'red' if val is False else 'black'
return 'color: %s' % color

并在您的DF中

df.style.apply(color_false_red, subset=['diff'])

这将更改文本的颜色。

最新更新