【发布时间】:2018-12-19 09:40:48
【问题描述】:
如果单元格大于取决于列标题的值,我想突出显示它们。
我想“读取”列标题,如果它在字典 (CEPA_FW) 中,则返回相应的值。然后,如果该列中的任何单元格大于此值,则将它们填充为深橙色。我的努力低于,但我收到错误(ValueError:长度不匹配:预期轴有 1 个元素,新值有 4 个元素)。
df=pd.DataFrame(({'As':['0.001', 0, '0.001','0.06'], 'Zn': ['6','4','6','8'], 'Pb': ['0.006','0','0.005','0.005'], 'Yt': [1,0,0.002,6]}))
cols=df.columns
CEPA_FW= {'Ag':0.05,'As' :0.05 ,'Ba':1.0,'B':1.0,'Cd' :0.01 ,'Cr' :0.05 ,'Co':0.001,'Cu' :1.0 ,'K':5.0,'Pb' :0.005 ,'Hg' :0.0002 ,'Mn':0.5,'Ni' :1.0 ,'Se':0.01,'Sn':0.5,'SO4':400.0,'Zn' :5.0}
def fill_exceedances(val):
for header in cols:
if header in CEPA_FW:
for c in df[header]:
fill = 'darkorange' if c> CEPA_FW[header] else ''
return ['backgroundcolor: %s' % fill]
df.style.apply(fill_exceedances, axis = 1).to_excel('styled.xlsx', engine='openpyxl')
【问题讨论】:
-
appropriate limit is returned这是什么意思? -
@RahulAgarwal,我已经编辑得更清楚了。
标签: python pandas conditional-formatting