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?

E_net4
  • 27,810
  • 13
  • 101
  • 139
PanLondon
  • 19
  • 2

1 Answers1

0

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:

Use the subset argument to apply, as suggested in

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') ])

(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') ])
E_net4
  • 27,810
  • 13
  • 101
  • 139
OCa
  • 298
  • 2
  • 13