您好朋友,我想对包含数字和非数字列类型的数据框应用样式功能。如何避免以下错误?
错误: TypeError:(“'>'在'str'和'int'的实例之间不支持”,'发生在索引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)