'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?



Solution 1:[1]

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)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1