Ron I
Ron I

Reputation: 4250

DataFrame pandas styling using applymap() not working in class, only in Jupyter cell

When I run applymap() in a Jupyter cell, it works fine. However, when I run the exact same code inside of my class, it doesn't style the DataFrame.

this code works as expected

#get the DataFrame from the class in the Jupyter cell
df = my_class.quality('headers')

# applymap() styles the table outside the class as expected
df.style.applymap(my_class.quality_style_null_val)

enter image description here

However, calling applymap inside the class doesn't style table

#functions in my_class
 def quality(self, key=None):    
      df = self.df(key)      
      df.style.applymap(self.test)      
      return df
  
  def test(val, params={'background-color':'green', "color":'white'}):        
    return 'color: red'
        

in Jupyter cell

#instantiate my_class and call function:

df = my_class.quality('headers')

enter image description here

Upvotes: 0

Views: 460

Answers (1)

Ron I
Ron I

Reputation: 4250

It works when you return the df.applymap() instead of styling and then returning.

def quality(self, key):        
    df = unify.df(key)    
    return df.style.applymap(self.quality_style)      

Upvotes: 3

Related Questions