How can I style specific cells in a pandas dataframe, based on the value of the adjacent cells?

Question:

I have a pandas dataframe in which, for some cells (mostly columns), I’d like to highlight any cells which are different than the cells to their right.

I’ve looked at all manner of pandas styler info and can’t work out how to apply conditional formatting based on the value of cells in a particular relative position.

Can someone point me in the right direction? All of the answers I can find on here pertain to applying conditional formatting based on aggregate values.

I’ve tried nothing and I’m all out of ideas – where do I start?

edit – Here’s an example of the use case: I am doing most of my manipulation using numpy arrays, and then converting this to a dataframe for the purpose of exporting it to excel.

For a dataframe arrayed like this:

[[A, 1, 1, 2, 1],  
 [B, 1, 2, 3, 3],  
 [C, 3, 3, 1, 1]
 ]

I would want a style to be applied only to cells [0,3], [0,4] (don’t match), and then [1,1], [1,2] (don’t match). In other words, the tests could be run only on columns 1 and 3, in which they are compared to the adjacent values in columns 2 and 4, 4 respectively, and then the styler applied to the pairs of cells which are not the same.

[[A, 1, 1, **2**, **1**],  
 [B, **1**, **2**, 3, 3],  
 [C, 3, 3, 1, 1]
 ]

Ideally, the solution would just be a function which accepts a range of cells, and then for each of those cells, highlights it if the cell to the right has a different value. I could then call it for each column only where I needed it. I don’t need a function that works columnwise or on the entire dataframe etc as I have header rows with various info.

Asked By: Nathan

||

Answers:

IIUC, you can use:

import numpy as np

def color_right(df, exclude=None):
    cols = df.columns.difference([0]) if exclude else df.columns
    m = df[cols].ne(df[cols].shift(axis=1))
    return np.where((m&m.shift(-1, axis=1, fill_value=True)
                    ).reindex(df.columns, axis=1, fill_value=False),
                    'background-color: yellow', '')

df.style.apply(color_right, exclude=[0], axis=None)

output:

enter image description here

Answered By: mozway
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.