0

Suppose that we have this dataframe:

Value 1 Value 2 Value 3
6 5 6
6 5 10

How to apply color if values on the same column is equals but skip the first column? In my case, column Value 2 must be colored

I have used Pandas df.apply, but the example is compare column on the same row

3 Answers 3

1

Hi, you can try like this one:

df = df.style.apply(lambda x: ['background-color: lightgreen']*len(df) if (x.iloc[0] == x.iloc[1] and x.name != 'Value1') else ['background-color: white']*len(df), axis = 0)
Sign up to request clarification or add additional context in comments.

Comments

1

You can use:

def color(s):
    return s.duplicated(keep=False).map({True: 'background-color: yellow'})

df.style.apply(color, subset=df.columns[1:], axis=0)

output:

enter image description here

Comments

0

Use:

df.style.apply(lambda x: np.where(np.ones(len(x), dtype=bool) * x.nunique() == 1, 
              'background-color: yellow', ''), 
               subset=df.columns[1:], 
               axis=0)

2 Comments

@yunastrian - Are you sure solution is correct? First column is always Value 1 ? DataFrame has always 2 rows?
Yes it solve my problem. My dataframe has always 2 rows, And I rename the column name "Value 1" to the right column nime

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.