I have a dataframe with several columns and a list with colors associated with each column. I want to highlight the non-blank cells in each column with the associated color.
I've tried iterating over the columns in various ways. The closest thing to success was to put a for loop in the styling function and apply it within a for loop. This correctly highlights the last column, but not the rest.
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])
