Search code examples
pythonpandaspandas-styles

How to use Pandas stylers for coloring an entire row based on a given column?


I've been trying to print out a Pandas dataframe to html and have specific entire rows highlighted if the value of one specific column's value for that row is over a threshold. I've looked through the Pandas Styler Slicing and tried to vary the highlight_max function for such a use, but seem to be failing miserably; if I try, say, to replace the is_max with a check for whether a given row's value is above said threshold (e.g., something like

is_x = df['column_name'] >= threshold

), it isn't apparent how to properly pass such a thing or what to return.

I've also tried to simply define it elsewhere using df.loc, but that hasn't worked too well either.

Another concern also came up: If I drop that column (currently the criterion) afterwards, will the styling still hold? I am wondering if a df.loc would prevent such a thing from being a problem.


Solution

  • This solution allows for you to pass a column label or a list of column labels to highlight the entire row if that value in the column(s) exceeds the threshold.

    import pandas as pd
    import numpy as np
    
    np.random.seed(24)
    df = pd.DataFrame({'A': np.linspace(1, 10, 10)})
    
    df = pd.concat([df, pd.DataFrame(np.random.randn(10, 4), columns=list('BCDE'))],
                   axis=1)
    df.iloc[0, 2] = np.nan
    
    def highlight_greaterthan(s, threshold, column):
        is_max = pd.Series(data=False, index=s.index)
        is_max[column] = s.loc[column] >= threshold
        return ['background-color: yellow' if is_max.any() else '' for v in is_max]
    
    
    df.style.apply(highlight_greaterthan, threshold=1.0, column=['C', 'B'], axis=1)
    

    Output:

    enter image description here

    Or for one column

    df.style.apply(highlight_greaterthan, threshold=1.0, column='E', axis=1)
    

    enter image description here