我想给矿井数据框中的负数一个红色。但是当尝试使用以下代码实现时
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
s = df05.style.applymap(color_negative_red)
print(s)
我收到以下值错误"值错误:非唯一索引不支持样式。
我必须在哪里寻找才能获得正确的输出?
我相信
您需要通过DataFrame.reset_index
和drop=True
的唯一默认索引值:
s = df05.reset_index(drop=True).style.applymap(color_negative_red)
这可能是
两个可能的问题之一,要么你的索引有重复的名称(检查df.index
(,要么你的列有重复的名称(检查df.columns
(。
如果索引有重复项,请执行以下操作:
df = df.reset_index(drop=True)
如果您的列有重复项,请执行以下操作:
df.columns = range(len(df.columns))