Search code examples
pythonpandasformattingstyles

What is the step by step explanation for pandas style highlighting format?


This is a very newbie question, but most tutorials are assuming students understand this, but I'm not. I found this example of pandas formatting style:

def highlight_max(s):
    is_max = s == s.max()
    return ['background-color: green' if v else '' for v in is_max]

This function paints the cell containing the highest value in the column. I didn't understand what this is doing. I know is a broad question, but I really wanted to understand the steps in this function.


Solution

  • Let's say you have the following random dataframe

    import pandas as pd
    import numpy as np
    df = pd.DataFrame(np.random.rand(5,3), columns=list('ABC'))
    df
    

    enter image description here

    Now, let take a look at this function you have

    def highlight_max(s):
        is_max = s == s.max()
        return ['background-color: green' if v else '' for v in is_max]
    

    so, basically, this function takes each column and mark the largest value of each column. Let's apply it on your dataframe and see the output:

    df.apply(highlight_max)
    

    enter image description here

    as you can see, we have another dataframe with the same size as df. All values are empty string except the max value in each column has as string 'background-color: green'.

    Now we apply this new dataframe on the original dataframe but with styling function.

    df.style.apply(highlight_max)
    

    enter image description here

    That's it. Each cell with no style is the same as before and each cell with 'background-color: green' is green.