Search code examples
pythonpandasdataframecolorspython-applymap

Python: How to Apply Style on Pandas Dataframe by Comparing Two Values on the Same Column but on the Different Rows?


Suppose that we have this dataframe:

Value 1 Value 2 Value 3
6 5 6
6 5 10

How to apply color if values on the same column is equals but skip the first column? In my case, column Value 2 must be colored

I have used Pandas df.apply, but the example is compare column on the same row


Solution

  • Hi, you can try like this one:

    df = df.style.apply(lambda x: ['background-color: lightgreen']*len(df) if (x.iloc[0] == x.iloc[1] and x.name != 'Value1') else ['background-color: white']*len(df), axis = 0)