gpt4 book ai didi

python-3.x - Pandas 样式标签给 "ValueError: style is not supported for non-unique indices"

转载 作者:行者123 更新时间:2023-12-04 12:18:24 24 4
gpt4 key购买 nike

我想给我的数据框中的负数一个红色。
但是当尝试使用以下代码实现时

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)

我收到以下值错误“ValueError:非唯一索引不支持样式。”

我必须在哪里寻找正确的输出?

最佳答案

我相信您需要唯一的默认索引值 DataFrame.reset_index drop=True :

s = df05.reset_index(drop=True).style.applymap(color_negative_red)

关于python-3.x - Pandas 样式标签给 "ValueError: style is not supported for non-unique indices",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55430318/

24 4 0