我有一个包含几列的数据框和一个包含与每列关联的颜色的列表。我想用相关的颜色突出显示每列中的非空白单元格。
我尝试过以各种方式迭代列。最接近成功的事情是在样式函数中放置一个 for 循环并将其应用到一个 for 循环中。这正确地突出了最后一列,但不是其余的。
df=pd.DataFrame({'a':[1,2,3,4],'b':['','',1,''],'c':['a','b','c','']})
df_column_colors=['red','blue','green']
def highlight_cells(value):
if value=='':
background_color=None
else:
for v in range(len(df_column_colors)):
background_color=str(df_column_colors[v])
return 'background-color: %s' % background_color
for i in range(len(df.columns)):
df2=df.style.applymap(highlight_cells,subset=df.columns[i])