4

I am trying to highlight the single whole cell in pandas based on text. For example, if Recommend is 'SELL', I want to highlight in red and green for 'BUY'. Appreciate if someone can guide me on this.

def color_negative_red(value):
    if value < 0:
        color = 'red'
    elif value > 0:
        color = 'green'
    else:
        color = 'black'
    return 'color: %s' % color

import pandas as pd

data = {'Stock': ['TSLA','GM','GOOG','MMM'],
        'Diff': [-200,-50,150,50],
        'Recommend' : ['SELL','SELL','BUY','BUY']
        }

df = pd.DataFrame(data, columns = ['Stock', 'Diff', 'Recommend'])

df.style.applymap(color_negative_red, subset=['Diff'])

### how to get a conditional highlight based on 'Recommend' ?????

2 Answers 2

5

Styles can be chained together. There are many ways to solve this problem, assuming 'BUY' and 'SELL' are the only options np.where + apply is a good choice:

def color_recommend(s):
    return np.where(s.eq('SELL'),
                    'background-color: red',
                    'background-color: green')

(
    df.style.applymap(color_negative_red, subset=['Diff'])
        .apply(color_recommend, subset=['Recommend'])
)

Alternatively in a similar way to color_negative_red:

def color_recommend(value):
    if value == 'SELL':
        color = 'red'
    elif value == 'BUY':
        color = 'green'
    else:
        return
    return f'background-color: {color}'


(
    df.style.applymap(color_negative_red, subset=['Diff'])
        .applymap(color_recommend, subset=['Recommend'])
)

styled frame

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

Comments

3

You are almost there!

def color_negative_red(value):
    if value < 0:
        color = 'pink'
    elif value > 0:
        color = 'lightgreen'
    else:
        color = 'white'
    return 'background-color: %s' % color

import pandas as pd

data = {'Stock': ['TSLA','GM','GOOG','MMM'],
        'Diff': [-200,-50,150,50],
        'Recommend' : ['SELL','SELL','BUY','BUY']
        }

df = pd.DataFrame(data, columns = ['Stock', 'Diff', 'Recommend'])

df.style.applymap(color_negative_red, subset=['Diff'])

Only change needed is color needs to become background-color: return 'background-color: %s' % color

df looks pretty

If you wanted to highlight the entire row, try:

def color_negative_red(row):
    print(row)
    value = row.loc["Diff"]
    if value < 0:
        color = 'pink'
    elif value > 0:
        color = 'lightgreen'
    else:
        color = 'black'
    return ['background-color: %s' % color for r in row]

import pandas as pd

data = {'Stock': ['TSLA','GM','GOOG','MMM'],
        'Diff': [-200,-50,150,50],
        'Recommend' : ['SELL','SELL','BUY','BUY']
        }

df = pd.DataFrame(data, columns = ['Stock', 'Diff', 'Recommend'])

df.style.apply(color_negative_red, axis=1)

df prettiness intensifies

1 Comment

A couple of years later and I am realising I did not answer exactly what was asked.

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.