1

I'm trying to highlight specific cells for each column with different condition which their value matches the condition for each row.

Below image is what I want to achieve: The table I attempt to achieve

I searched google and stackoverflow but none of these can meet my requirement. Can anyone who's familiar with Pandas Style could assist?

Below are the codes I tried and failed:

Ex1

import pandas as pd
df = pd.DataFrame([[10,3,1], [3,7,2], [2,4,4]], columns=list("ABC"))

def highlight(s):
    return ['background-color: yellow' if (v>2) else 'background-color: white' for v in s]
df.style.apply(highlight, axis=0)

Ex2

import pandas as pd
df = pd.DataFrame([[10,3,1], [3,7,2], [2,4,4]], columns=list("ABC"))

Column_limit = (df['A'] > 6) | (df['B'] > 2) | (df['C'] < 3)
df[Column_limit].style.applymap(lambda x: 'background-color: yellow', subset=pd.IndexSlice[:, ['A', 'C']])

Ex3

import pandas as pd
df = pd.DataFrame([[10,3,1], [3,7,2], [2,4,4]], columns=list("ABC"))

subsets = pd.IndexSlice[:, 'A']
df.style.applymap(lambda x: 'background-color: yellow', subset = subsets)
2
  • There are only 3 columns? Commented Jul 15, 2021 at 6:43
  • In my real case, there are many columns. I just posted a simplified example here. Your second answer meets my requirement too. Appreciate! Commented Jul 15, 2021 at 7:05

1 Answer 1

0

If there is same number of conditions like some number of columns use:

df = pd.DataFrame([[10,3,1], [3,7,2], [2,4,4]], columns=list("ABC"))

def highlight(x):
    c1 = 'background-color: yellow'

    # condition
    m = pd.concat([(x['A'] > 6), (x['B'] > 2), (x['C'] < 3)], axis=1)
    #print (m)
    #empty DataFrame of styles
    df1 = pd.DataFrame('', index=x.index, columns=x.columns)
    #set new columns by condition
    return df1.mask(m, c1)


df.style.apply(highlight, axis=None)

If there is a lot of columns and need processing only some of them:

def highlight(x):
    c1 = 'background-color: yellow'

    #empty DataFrame of styles
    df1 = pd.DataFrame('', index=x.index, columns=x.columns)
    #set new columns by condition
    
    df1.loc[(x['A'] > 6), 'A'] = c1
    df1.loc[(x['B'] > 2), 'B'] = c1
    df1.loc[(x['C'] < 3), 'C'] = c1
    
    return df1

df.style.apply(highlight, axis=None)

EDIT:

If need specified all masks but in last step filter only some columns use:

def highlight(x):
    c1 = 'background-color: yellow'

    #empty DataFrame of styles
    df1 = pd.DataFrame('', index=x.index, columns=x.columns)
    #set new columns by condition
    
    df1.loc[(x['A'] > 6), 'A'] = c1
    df1.loc[(x['B'] > 2), 'B'] = c1
    df1.loc[(x['C'] < 3), 'C'] = c1
    
    need = ['A','C']
    df1 = df1[need].reindex(x.columns, fill_value='', axis=1)
    return df1

Or remove masks which not necessary:

def highlight(x):
    c1 = 'background-color: yellow'

    #empty DataFrame of styles
    df1 = pd.DataFrame('', index=x.index, columns=x.columns)
    #set new columns by condition
    
    df1.loc[(x['A'] > 6), 'A'] = c1
    df1.loc[(x['C'] < 3), 'C'] = c1
    
    return df1

df.style.apply(highlight, axis=None)
Sign up to request clarification or add additional context in comments.

5 Comments

Wow, thank you so much @jezrael for the prompt response. This is exactly what I want! I spent a few days on this question, you did save a lot of my time. May I have a further question. If I exclude the condition (greater than 2) for column B and I only want to display the rows who matches conditions for column A and C (greater than 6 and less than 3 respectively). For example, it will only display two rows with index 0 and 1 in my case, like the image here link. How can I add a filter in my code?
@KelvinLo - So it means removed df1.loc[(x['B'] > 2), 'B'] = c1 ?
Yes, remove the condition for Column B, but not to display the third row (index 2). Only show the rows with highlighted number. Just like this image link
@KelvinLo - Not underatand, need remove styles for column B? In picture are grey color, it is necessary?
My apology for the grey picture, re-uploaded new one. link. If I don't apply the condition to Column B and I want to only display the row who matches the condition for Column A and C.

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.