2 回答

TA貢獻(xiàn)1946條經(jīng)驗(yàn) 獲得超4個(gè)贊
創(chuàng)建另一個(gè)掩碼并以相同的方式傳遞,同樣對(duì)于默認(rèn)空值使用DataFrame構(gòu)造函數(shù):
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貢獻(xiàn)1820條經(jīng)驗(yàn) 獲得超10個(gè)贊
另一種方法是定義一個(gè)函數(shù),這樣你就可以在行上應(yīng)用:
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)
輸出:
添加回答
舉報(bào)