0

How to apply conditional formatting (highlight in yellow if value > 1.), not over the whole table but only these 2 columns:

  • "comparisonResult.NoMatch"
  • "False"

I can only apply to the whole table. enter image description here

I have this code:

styled_df = result.loc[:,idx[:,'ComparisonResult.NoMatch']]
                  .style.apply(lambda x : ['font-weight: bold; background-color: yellow' 
                                          if value >= 1 else '' for value in x])

But this basically leaves me with only my ComparisonResult.NoMatch Column as my result.

By the way, I am using Visual Studio Code, but I don't see very rich IntelliSense, for example pressing dot on the "value" field suggests nothing. Is something wrong with my extensions installation?

2

1 Answer 1

1

Issues with your suggested code:

  • Indeed a .loc on the dataframe to be formatted will only limit your view, not select the region to apply format to.
  • The origin and purpose of idx are unclear.

This is what you need instead:

df.style.apply(lambda x: ['font-weight: bold; background-color: yellow' if value >= 1 else '' for value in x], 
               subset=[ ('Match','comparisonResult.NoMatch'), ('Match','False') ])

This uses the subset argument to apply, as suggested in

(It seems like result may be the name of your multi-index dataframe, prior to formatting, but I'm not sure. I've called it df. )

By the way, for easier reuse you may declutter like this:

Import numpy as np

def highlighter(x):
    return np.where(x>=1, 'font-weight: bold; background-color: yellow', None)

df.style.apply(highlighter, 
               subset=[ ('Match','comparisonResult.NoMatch'),
                        ('Match','False') ])
Sign up to request clarification or add additional context in comments.

Comments

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.