Wolfy
Wolfy

Reputation: 458

Given pandas dataframe highlight one column

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.

Upvotes: 0

Views: 887

Answers (2)

O.O
O.O

Reputation: 1298

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)

Upvotes: 1

Elias Kassell
Elias Kassell

Reputation: 180

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.

Upvotes: 3

Related Questions