【发布时间】:2020-02-06 18:04:48
【问题描述】:
朋友们好, 我想在包含数字和非数字列类型的数据框中应用样式函数。 如何避免以下错误?
错误: TypeError: ("'>' not supported between 'str' and 'int'", 'occured at index A')
import pandas as pd
d = {'A': ['A','B','C']}
data = pd.DataFrame(data=d)
data['B']=[7,8,9]
data['C']=[1,2,3]
def color_negative_red(val):
"""
Takes a scalar and returns a string with
the css property `'color: red'` for negative
strings, black otherwise.
"""
color = 'white' if (val > 2) else 'black'
return 'color: %s' % color
df_new = data.style.applymap(color_negative_red)
【问题讨论】:
标签: python-3.x pandas pandas-styles