Pandas style tag gi...
 
Share:
Notifications
Clear all

Pandas style tag give “ValueError: style is not supported for non-unique indices”


Neha
 Neha
(@asha)
Member Admin
Joined: 4 years ago
Posts: 31
Topic starter  

I would like to give the negative numbers in my 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?


Govind and myTechMint liked
Quote
Topic Tags
myTechMint
(@mytechmint)
Member Moderator
Joined: 4 years ago
Posts: 52
 

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)

Neha and Govind liked
ReplyQuote