pandas 熊猫样式标签给出“ValueError:非唯一索引不支持样式”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/55430318/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
pandas style tag give "ValueError: style is not supported for non-unique indices"
提问by Metroll
I would like to give the negative numbers in mine data frame a red color. But when trying to achieve with the following code
我想给我的数据框中的负数一个红色。但是当尝试使用以下代码实现时
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)
I got the following Value Error "ValueError: style is not supported for non-unique indices."
我收到以下值错误“ValueError:非唯一索引不支持样式。”
Where must i look to get the right output?
我必须在哪里寻找正确的输出?
回答by jezrael
I believe you need unique default index values by DataFrame.reset_index
and drop=True
:
我相信你需要独特的默认索引值DataFrame.reset_index
和drop=True
:
s = df05.reset_index(drop=True).style.applymap(color_negative_red)