[英]Problem with Styling withing pandas dtaframe: AttributeError: 'float' object has no attribute 'max'
我试图在google colab notebook<\/a>中突出显示切片数据框中<\/a>的最小值,如下所示:
def color_negative_red(val):
"""
Takes a scalar and returns a string with
the css property `'color: red'` for negative
strings, black otherwise.
"""
color = 'red' if val < 0 else 'black'
return 'color: %s' % color
def highlight_max(s):
'''
highlight the maximum in a Series yellow.
'''
is_max = s == s.max()
return ['background-color: yellow' if v else '' for v in is_max]
#def highlight_max(s, props=''):
# return np.where(s == np.nanmax(s.values), props, '')
dff = pd.DataFrame({'A': np.linspace(1, 10, 10)})
dff = pd.concat([dff, pd.DataFrame(np.random.randn(10, 4), columns=list('BCDE'))],
axis=1)
dff.style.\
applymap(color_negative_red , subset=['B', 'C', 'D']).\
applymap(highlight_max, subset=['B', 'C', 'D'])
以下是您可以执行的操作:
dff.style.\
applymap(color_negative_red , subset=['B', 'C', 'D']).\
apply( highlight_max, subset=['B', 'C', 'D'])
.applymap
按元素应用函数,而.apply
将函数应用于系列。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.