3

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

4
  • The error looks clear... your data frame has a non-unique index (that is, the index has repeated values), and .style is not supported for that case. You can try with df.reset_index().style.apply(highlight_rows, axis = 0). Commented Apr 5, 2018 at 16:27
  • Thank you @jdehesa, that got rid of the error. I thought was something else because I tried to use the command 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. Commented Apr 5, 2018 at 16:47
  • well reset_index() gives you a new data frame, you can do df2 = df.reset_index(); df2.style.apply(highlight_rows, axis = 0) and then export df2... Commented Apr 5, 2018 at 17:40
  • 1
    Info: Two columns with the same name generate the same error message. Commented Jan 29, 2019 at 9:36

2 Answers 2

2

The apply method extracts each column or row depend on axis=0 or axis=1. Then you can add any style for each cell in rows or columns. If you want to pass your style through method, you need to assign the method expression for each element of array. Otherwise, it must be None value.

def highlight_rows(s):
    con = s.copy()
    con[:] = None
    if (s['my_column'] == 'some_text'):
        con[:] = "background-color: green"
    elif (s['my_column'] == 'somedifferent_text'):
        con[:] = "background-color: blue"
    return con
        
df.style.apply(highlight_rows, axis=1)
Sign up to request clarification or add additional context in comments.

Comments

0

assuming s is equal to your dataframe : Try this:

def file():
    styled = df.style.apply(highlight_rows, axis = 0)
    f = open('new_file.html', 'w')
    html = (styled.render())
    f.write(html)
    f.close()

def highlight_rows(s):        
    if s.my_column == 'some_text':
        return ['background-color: green'] * s.size
    elif s.my_column == 'somedifferent_text':
        return ['background-color: blue'] * s.size

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.