0

I want to highlight some values in some columns of a pandas dataframe. I have this code to do it: Sample dataframe:

    Name    ID Name_1  ID.1
0    cat  98.4    dog  98.4
1  cat.1  96.5  dog.1  96.5
2  cat.3  95.4  dog.3  95.4

def color_negative_red_1(value):
    if value <= 96.5:
        color='red'
    elif value == 97.5:
        color='blue'
    else:
        color='black'
    return ['background-color: red']

df_1.style.applymap(color_negative_red_1,subset=['ID','ID.1'])

Here is how it should look:

enter image description here

but there aren't any changes in my excell file...

1
  • why isnt 96.5 highlighted since your first condition says it should be red: <= 96.5 means less than equal to 96.5 Commented Jan 31, 2020 at 17:12

1 Answer 1

2
  1. applymap works elementwise, you don't need to return list, it should be just the string.
  2. You are always setting the color to red, you are not using the variable color
def color_negative_red_1(value):
    if value <= 96.5:
        color='red'
    elif value == 97.5:
        color='blue'
    else:
        color='black' # a really bad idea, may be you should just set color = ''
    return 'background-color: {}'.format(color)
Sign up to request clarification or add additional context in comments.

2 Comments

I don't know, I can't edit my excell in ordert to have this output
Good point , though you could just use color='' under else for no colors :)

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.