import pandas as pd
import numpy as np
ex1 = pd.DataFrame([
[1, 2, 3],
[2, 1, 4],
[5, 3, 1]
], list('ABC'), list('XYZ'))
ex2 = pd.DataFrame([
[1, 2, 3],
[1, 9, 4],
[5, 3, 1]
], list('ABC'), list('XYZ'))
def hl(x):
r = 'background-color: red'
g = 'background-color: green'
c = g if x.iloc[1, 1] < x.iloc[1, 0] else r
y = pd.DataFrame('', index=x.index, columns=x.columns)
y.iloc[1, 1] = c
return y
ex1.style.apply(hl, axis=None)

ex2.style.apply(hl, axis=None)

source
share