Zach Mandell
Zach Mandell

Reputation: 13

Pandas Multi-Index DataFrame styling without using apply() or applymap()

I'm trying to highlight or emphasize the "Total" rows in my DataFrame. It looks like using apply() or applymap() is the most common way people have done conditional formatting but I'd like to simply identify rows to format using multi-index slicing. Have been stuck for a while here and could use some help. I'm doing this within Mode analytics, but I don;t think this should affect the approach.

I'm trying to incrementally get to a solution by using applymap() to make all text in a slice red but it is not rendering when table is shown.

Code View 1 DataFrame

def color_positive_red(val):
"""
Takes a scalar and returns a string with
the css property `'color: red'` for negative
strings, black otherwise.
"""
color = 'red' if val > 0 else 'black'
return 'color: %s' % color


final.style.applymap(color_negative_red,
              subset=final.loc[('1. Leads','Total')])

Upvotes: 0

Views: 637

Answers (0)

Related Questions