I have been trying to highlight some rows in a pandas dataframe based on multiple conditions.
I'm expecting that when a string in the target column match the criteria defined in the function, the entire row will be highlighted.
I tried different combinations of the .style.apply method, but it kept giving me the following error:
ValueError: style is not supported for non-unique indicies.
This is the code:
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)
I'm using Python 3.6.5 and Pandas 0.22.0
Any idea on what I'm doing wrong?
Should I pass different parameters or doing a different loop?
Thank you
.styleis not supported for that case. You can try withdf.reset_index().style.apply(highlight_rows, axis = 0).df.set_index(['my_column'], append = True)first. It's not giving me any error right now but I don't see any row highlighted after exporting the dataframe using the ExcelWriter method.reset_index()gives you a new data frame, you can dodf2 = df.reset_index(); df2.style.apply(highlight_rows, axis = 0)and then exportdf2...