0
A B C
1 jack has a cat jack aa
2 jim has a car jack ab
3 jack has a dog jim cc
4 jack has a house jim dd

I want to high light cell that got Jack with color red, Jim with yellow what I came up with but have no outcome

def highlight_jack(ser):
       color= "red" if 'jack' in str(ser)  else ''
       return f"background-color: {color}"

def highlight_jim(ser):
       color= "yellow" if 'jim' in str(ser)  else ''
       return f"background-color: {color}"

df_merge.style.applymap(highlight_jack, subset=['B'])
df_merge.style.applymap(highlight_jim, subset=['B'])

df_merge.to_excel(writer, sheet_name= sheet, index=False)
writer.save()
Kan
  • 1
  • 2
  • Does this answer your question? [Conditionally format Python pandas cell](https://stackoverflow.com/questions/41203959/conditionally-format-python-pandas-cell) – Tomerikoo Dec 08 '22 at 12:10
  • 1
    Jack has a capital J in the name. You would probably want to test if 'jack' in str(ser).lower() – Lexpj Dec 08 '22 at 12:18
  • @Tomerikoo `df_merge.style.apply(lambda x: ["background: red" if 'jack' in str(v) else "" for v in x], axis = 1, subset['B'])` is this the right approach ? Still no outcome – Kan Dec 08 '22 at 12:21
  • @Lexpj Just fixed the table, text are matched – Kan Dec 08 '22 at 12:23

0 Answers0