我一直在尝试根据多个条件在pandas数据帧中突出显示某些行。
我期望当目标列中的字符串符合函数中定义的条件时,整行都会被突出显示。
我尝试了不同组合的.style.apply方法,但它一直给我以下错误:
ValueError: style is not supported for non-unique indicies.
这是代码:
我期望当目标列中的字符串符合函数中定义的条件时,整行都会被突出显示。
我尝试了不同组合的.style.apply方法,但它一直给我以下错误:
ValueError: style is not supported for non-unique indicies.
这是代码:
def highlight_rows(s):
if s['my_column'] == 'some_text':
return 'background-color: green'
elif s['my_column'] == 'somedifferent_text':
return 'background-color: blue'
df.style.apply(highlight_rows, axis = 0)
我正在使用Python 3.6.5和Pandas 0.22.0。
你有什么想法,我做错了什么吗?
我应该传递不同的参数或者使用不同的循环吗?
谢谢。
.style
不支持该情况。您可以尝试使用df.reset_index().style.apply(highlight_rows, axis = 0)
。 - undefineddf.set_index(['my_column'], append = True)
。现在没有报错了,但是在使用ExcelWriter方法导出数据框后,我没有看到任何行被突出显示。 - undefinedreset_index()
方法会返回一个新的数据框,你可以使用df2 = df.reset_index(); df2.style.apply(highlight_rows, axis = 0)
来创建df2
并应用highlight_rows
样式,然后导出df2
。 - undefined