是否可以在为列名添加背景色的同时还根据单独的条件更改单元格背景色?
我目前能够根据条件突出显示单元格,但不确定如何为列名添加背景色:
# create dataframe
import pandas as pd
data = {'Cuisine':['Italian','Indian','Nepalese','Mexican', 'Thai'],
'Geographic Location':['Europe','Asia','Asia','N.America','Asia']}
df = pd.DataFrame(data) print(df)
Cuisine Geographic Location
0 Italian Europe
1 Indian Asia
2 Nepalese Asia
3 Mexican N.America
4 Thai Asia
# highlight cells based on condition
def highlight_Asia(x):
return ['background-color: GreenYellow' if v =='Asia' else '' for v in x]
df.style.apply(highlight_Asia)
# highlight column names
def highlight_header(x):
y= ['background-color: LightSkyBlue' for v in list(x)]
return y
df.style.apply(highlight_header)