2 回答
TA贡献1946条经验 获得超4个赞
创建另一个掩码并以相同的方式传递,同样对于默认空值使用DataFrame构造函数:
def highlight_col(x):
df = pd.DataFrame('', index=x.index, columns=x.columns)
mask1 = x['name'] == x['Perceived OoM.1']
mask2 = x['name'] == x['Perceived OoM.2']
df.loc[mask1, :] = 'background-color: yellow'
df.loc[mask2, :] = 'background-color: blue'
return df
TA贡献1820条经验 获得超10个赞
另一种方法是定义一个函数,这样你就可以在行上应用:
def highlight(x):
color = 'background-color:yellow' if x['name']==x['Perceived OoM.1']\
else 'background-color: green' if x['name']==x['Perceived OoM.2']\
else ''
return [color]*len(x)
df.style.apply(highlight, axis=1)
输出:

添加回答
举报
