0

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.

2 Answers 2

3
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.

Sign up to request clarification or add additional context in comments.

2 Comments

For some reason it didnt color the column :(
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!
1

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)

2 Comments

I am using VS Code and it still does not work but it does work with jupyter notebook :(
Check this link to work with Jupiter within VS code: code.visualstudio.com/docs/python/jupyter-support

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.