I want apply some filter conditions on specific cols. Based on filter I want to highlight that cell.
I took this example from another post:
styled = (df.style
.applymap(lambda v: 'background-color: %s' % 'green' if v=='col' else ''))
styled.to_excel('d:/temp/styled.xlsx', engine='openpyxl')
But problem with this is it applies to every cell. So I instead used apply function on a series by converting it to df like below:
df['a'] =df[['a']].style
.apply(lambda v: 'background-color: %s' % 'green' if v=='col' else '')
In this case when I export df to excel it gives style object rather than values.
If I dont assign that styled result to df['a']
and directly convert to excel then I get desired result with style but then i will get only that column and not entire dataframe
Use subset
parameter for specify column(s) for apply styles:
f = lambda v: 'background-color: %s' % 'green' if v=='col' else ''
df = df.style.applymap(f, subset=['a'])
For negation use Index.difference
:
df = df.style.applymap(f, subset=df.columns.difference(['a']))