5

I would like to test if the values of a column are bigger than another specific value of the same data frame. If a value is bigger, I want to highlight this specific cell.

I tried this:

import pandas as pd

b = pd.DataFrame([[5,7,3],[2,3,4],[8,4,7]])

for i in range(0, len(b)):
    for j in range(0, len(b.columns)):
        if  b.iloc[i][j] >  b.iloc[2][j]:
            b.style.applymap(lambda  x: 'background-color : blue' if b.iloc[i][j] >  b.iloc[2][j] else '') 

b

So in this example I want to check if 5 or 7 is bigger than 3 (column 1), 2 or 3 bigger than 4 (column 2) and 8 or 4 is bigger than 7.

It doesn't color anything... I hope someone can help me. Thx in advance.

datapug
  • 2,261
  • 1
  • 17
  • 33
SeaSeaEss
  • 53
  • 1
  • 6
  • 1
    Does this answer your question? [Conditionally format Python pandas cell](https://stackoverflow.com/questions/41203959/conditionally-format-python-pandas-cell) – Shir Mar 15 '21 at 11:08
  • I've seen this question already… it basically the code is the same but in my case it is not working... – SeaSeaEss Mar 15 '21 at 11:46
  • Interesting.. Can you add a minimal runnable snippet of your code? – Shir Mar 15 '21 at 11:50
  • Sorry I had some Problems entering code down here. It's now in the main Question. – SeaSeaEss Mar 15 '21 at 12:06
  • I added a solution that works for me directly in a jupyter notebook. – Shir Mar 15 '21 at 12:44

1 Answers1

2

Try this solution:

import pandas as pd
import numpy as np

df = pd.DataFrame([[5,7,8],[2,3,4],[8,4,9]])

def highlight(s):
    '''
    highlight the maximum in a Series.
    '''
    is_max = s >= s[2]
    return ['background-color: blue' if v else '' for v in is_max]

df.style.apply(highlight, axis=0)

enter image description here

Note that the solution is based on the thread we discussed. The main change is the condition inside the highlight function. Using applymap works on a single cell each time, and has no access to its location in the dataframe. Using apply works on a single row each time and enables comparing to cell in the same row and different column.

Shir
  • 1,571
  • 2
  • 9
  • 27
  • But I don't want to highlight the maximum of each row. I want to highlight in a huge data frame column by column the values which are bigger than the last value in this column. And changing applymap tp apply doen't work either.. – SeaSeaEss Mar 15 '21 at 12:46
  • Sorry for misunderstanding. I changed the `axis` in the last line of code from `1` to `0`. This makes it work on columns rather than rows. – Shir Mar 15 '21 at 12:49
  • Now it Looks good! Hopefully I can adapt this to my data frame and use it in the loops.. Tank you! – SeaSeaEss Mar 15 '21 at 12:53
  • 1
    You don't need loops to run it, it implicitly contains them inside. Working with `for` loops on data frames, especially when they are large is usually a bad practice. Let me know if you have more questions. – Shir Mar 15 '21 at 12:54
  • Ooh you're true. Okay now I need to mark values bigger then last value of the column and smaller then second last value of the column and this column by column. I guess I Need a loop to automatically do this for every column? – SeaSeaEss Mar 15 '21 at 13:00
  • Take a look at [Pandas Style Guide](https://pandas.pydata.org/pandas-docs/stable/user_guide/style.html), they create an auxiliary function like the `highlight` function in our example, and apply them one after the other on the same data frame. If you don't manage by youself you may edit the question and I'll change the solution accordingly. – Shir Mar 15 '21 at 13:03
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/229936/discussion-between-shir-and-seaseaess). – Shir Mar 15 '21 at 13:03
  • okay thank you for all the informations, the codes and the link. I'll try by myself and eventually come back to you. :) – SeaSeaEss Mar 15 '21 at 13:04