In [1]:
import pandas as pd
pd.set_option("precision", 1)
In [2]:
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]})
In [3]:
df
Out[3]:
name age num_children num_pets bank_balance
0 alan 34 1 1 100.0
1 beth 12 0 0 10.0
2 charlie 43 2 1 -10.0
3 david 32 1 2 30.0
4 edward 77 6 0 30.0

elementwise

In [4]:
def even_number_background(cell_value):
    
    highlight = 'background-color: darkorange;'
    default = ''
    
    if type(cell_value) in [float, int]:
        if cell_value % 2 == 0:
            return highlight
    return default
In [5]:
styler = df.style.applymap(even_number_background)
styler
Out[5]:
name age num_children num_pets bank_balance
0 alan 34 1 1 100.0
1 beth 12 0 0 10.0
2 charlie 43 2 1 -10.0
3 david 32 1 2 30.0
4 edward 77 6 0 30.0

rowwise

In [6]:
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)
Out[6]:
name age num_children num_pets bank_balance
0 alan 34 1 1 100.0
1 beth 12 0 0 10.0
2 charlie 43 2 1 -10.0
3 david 32 1 2 30.0
4 edward 77 6 0 30.0

columnwise

In [7]:
def maximum_value_in_column(column):    
               
    highlight = 'background-color: palegreen;'
    default = ''

    maximum_in_column = column.max()
    
    # must return one string per cell in this column
    return [highlight if v == maximum_in_column else default for v in column]
    
df.style.apply(maximum_value_in_column, subset=['num_children', 'num_pets'], axis=0)
Out[7]:
name age num_children num_pets bank_balance
0 alan 34 1 1 100.0
1 beth 12 0 0 10.0
2 charlie 43 2 1 -10.0
3 david 32 1 2 30.0
4 edward 77 6 0 30.0

chained calls

In [8]:
# use red font for negative bank balances
# write the highest age in bold
# use light red background for zero values in num_children and num_pets

def red_font_negatives(series):
    highlight = 'color: red;'
    default = ''
    return [highlight if e < 0 else default for e in series]  

def bold_max_value_in_series(series):
    highlight = 'font-weight: bold;'
    default = ''
    
    return [highlight if e == series.max() else default for e in series]  

def red_background_zero_values(cell_value):
    highlight = 'background-color: red;'
    default = ''
    if cell_value == 0:
        return highlight
    else:
        return default     

(df
 .style
 .apply(red_font_negatives, axis=0, subset=['bank_balance'])
 .apply(bold_max_value_in_series, axis=0, subset=['age'])
 .applymap(red_background_zero_values))
Out[8]:
name age num_children num_pets bank_balance
0 alan 34 1 1 100.0
1 beth 12 0 0 10.0
2 charlie 43 2 1 -10.0
3 david 32 1 2 30.0
4 edward 77 6 0 30.0