Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas style tag give "ValueError: style is not supported for non-unique indices"

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."

Where must i look to get the right output?

like image 494
Metroll Avatar asked Mar 30 '19 10:03

Metroll


Video Answer


1 Answers

I believe you need unique default index values by DataFrame.reset_index and drop=True:

s = df05.reset_index(drop=True).style.applymap(color_negative_red)
like image 194
jezrael Avatar answered Sep 21 '22 13:09

jezrael