熊猫样式标签给出“ ValueError:非唯一索引不支持样式”

时间:2019-03-30 10:04:40

标签: python-3.x pandas

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

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:非唯一索引不支持样式。”

我应该在哪里获得正确的输出?

1 个答案:

答案 0 :(得分:0)

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

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