0

I have below two requirements, but in a first step, when I try to highlight the column, it throws an error 'list' object is not callable I am relatively new to python so unable to figure out what exactly causing the error. Any help will be appreciated

  1. Highlight the entire row when it gets a specific id.
  2. Add a comment in the “Comments” column when a row is highlighted

Dataframe

    ID  Name    Comments
0   11  item1
1   12  item2    
2   13  item3
3   14  item4
4   15  item5


def highlight_rows(s):        
    if s.my_column == 'some_text':
        return ['background-color: yellow'] * s.size

a=12


df[df["ID"] == a].style.apply(highlight_rows(a),color='Blue', axis=None)

MikeCode
  • 91
  • 11

1 Answers1

1

Method apply accepts a function as a first parameter, whilest you are trying to pass a result of a function call (and you are returning a list).

Actually, I don't think you need to pass a parameter there

To highlight a row:

def highlight_max(x):
    return ['background-color: yellow' for v in x]

df[df["ID"] == a].style.apply(highlight_max)

to create a comment:

df[df["ID"] == a]['Comment']='my comment'
Tatiana Goretskaya
  • 536
  • 10
  • 25
  • hit the error `TypeError: () got an unexpected keyword argument 'color'` – MikeCode Feb 06 '20 at 19:34
  • Yeah, editing my answer now. meanwhile this can be a good related reading https://stackoverflow.com/questions/706721/how-do-i-pass-a-method-as-a-parameter-in-python – Tatiana Goretskaya Feb 06 '20 at 19:36
  • That hit another error `AttributeError: ("'Series' object has no attribute 'my_column'", 'occurred at index ID')` – MikeCode Feb 06 '20 at 19:57
  • Nevermind, your code worked. I was passing wrong function in `apply`. Thank you @Tatiana Didik – MikeCode Feb 06 '20 at 20:05