Я думаю, что новый столбец не нужен, нужно DataFrame of styles
только по оригинальным DataFrame
столбцам и индексировать и устанавливать строки по условию с помощью mask
:
def highlight(x):
c1 = 'background-color: green'
c2 = 'background-color: red'
df1 = pd.DataFrame(c2, index=x.index, columns=x.columns)
df1 = df1.mask(df['sum_of_points'].map(should_colored).ffill(), c1)
#print (df1)
return df1
df.style.apply(highlight, axis=None)
Образец :
df = pd.DataFrame({'sum_of_points':[0,1,2,1,2,3,4,1,2,2,4,5,0,1,2],
'A':range(15)})
print (df)
A sum_of_points
0 0 0
1 1 1
2 2 2
3 3 1
4 4 2
5 5 3
6 6 4
7 7 1
8 8 2
9 9 2
10 10 4
11 11 5
12 12 0
13 13 1
14 14 2
def should_colored(sum_of_points):
if sum_of_points > 2:
return True
elif sum_of_points < 2:
return False
else:
return np.NaN
def highlight(x):
c1 = 'background-color: green'
c2 = 'background-color: red'
df1 = pd.DataFrame(c2, index=x.index, columns=x.columns)
df1 = df1.mask(x['sum_of_points'].map(should_colored).ffill(), c1)
return df1
df.style.apply(highlight, axis=None)
![pic](https://i.stack.imgur.com/DvDms.png)