import pandas as pd
df = pd.DataFrame({
"name": ["alan","beth","charlie","david", "edward"],
"age" : [34, 12, 43, 32, 77],
"num_children": [1, 0, 2, 1, 6],
"num_pets": [1, 0, 1, 2, 0],
"bank_balance": [100.0, 10.0, -10.0, 30.0, 30.0]})
def more_children_or_more_pets_background(row):
highlight = 'background-color: lightcoral;'
default = ''
# must return one string per cell in this row
if row['num_children'] > row['num_pets']:
return [highlight, default]
elif row['num_pets'] > row['num_children']:
return [default, highlight]
else:
return [default, default]
df.style.apply(more_children_or_more_pets_background, subset=['num_children', 'num_pets'], axis=1)