I have a pandas dataframe. I want to highlight one of the columns to say blue. I have tried doing this:
df['column'] = df.style.apply(lambda x: ['background: lightblue' if x.name == 'column' else '' for i in x])
But this does not work.
df.style.apply
method
Because of this you do not want to be assigning the column to be equal to it. The style.apply is done in place, so remove the assignment and just use
df.style.apply(lambda x: ['background: lightblue' if x.name == 'column'
else '' for i in x])
and it will style the column in place.
x.name == 'column' is important, make sure you have a column in the DataFrame with that value. If you still can't get it to work then share more info please!This solution also works.
def highlight(s):
same = s == df['column']
return ['background-color: lightblue' if x else '' for x in same]
df.style.apply(highlight)