我有一个df
,如下所示:
a b c d
NaN NaN NaN 14.534883720930232
NaN NaN NaN 76.30813953488372
23.837209302325583 6.8313953488372094 5.959302325581396 12.5
5.377906976744186 4.215116279069767 6.8313953488372094 13.80813953488372
8.13953488372093 8.13953488372093 5.523255813953488 1.0174418604651163
2.0348837209302326 11.19186046511628 2.616279069767442 1.8895348837209303
30.377906976744185 7.122093023255814 6.104651162790698 57.70348837209303
2.4709302325581395 1.1627906976744187 1.1627906976744187 2.9069767441860463
1.8895348837209303 5.087209302325581 2.0348837209302326 16.424418604651162
10.465116279069768 5.523255813953488 2.4709302325581395 1.308139534883721
3.633720930232558 4.069767441860465 0.7267441860465116 13.953488372093023
7.412790697674419 8.575581395348838 9.30232558139535 42.151162790697676
32.122093023255815 9.30232558139535 4.215116279069767 2.0348837209302326
我想知道是否有一种方法可以根据阈值条件来df
个单元格。
我想将大于10的值着色为红色,将小于10的值着色为绿色。而NaNs
为黑色。
有没有办法做到这一点?
我尝试做:
def color_negative_red(value):
if value < 10:
color = 'green'
elif value >= 10:
color = 'red'
else:
color = 'black'
return 'color: %s' % color
df.style.applymap(color_negative_red)
但这不起作用。
有什么建议吗?