【发布时间】:2022-04-14 20:00:07
【问题描述】:
我想将我的数据框中的负数设为红色。 但是当试图用下面的代码实现时
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:非唯一索引不支持样式。”
我必须从哪里获得正确的输出?
【问题讨论】:
-
张贴这个以防有人需要这个:我最近遇到了这个问题。经过多次尝试,我意识到数据框中列的名称是问题所在,因为它们不是唯一的。重命名列的简单命令解决了这个问题,接下来是一些示例: df.columns = range(df.size) df.columns = ['A', 'B', 'C'] df.T.reset_index(drop =真).T
标签: python-3.x pandas