Skip to content
Advertisement

Pandas Styler conditional formatting based on comparison of each row with last row

I have a large dataframe that comes from calculation with varying number of columns and rows:

JavaScript

Each column has last row that decides coloring of each cell in that column. Each cell of the column needs to be compared with the last cell of that particular column and then the condition to be applied is: if s>s[-1]: background-color: green, if s<s[-1]: background-color: red, if s==s[-1]: background-color: yellow.

To get output like this: enter image description here

Therefore I need to highlight all rows except last using the above conditions.

I tried with two conditions but could not get the third condition:

JavaScript

I don’t know how to use three condition using this function. Can some one help?

Advertisement

Answer

You can use:

JavaScript

Output:

pandas style difference row

Used input:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement