2021-07-28

change Pandas Row background colour and Font Colors based on 2 different conditions

I have a code to change the cell background colour if the Rising is False. I want to change the Cell Colour to Green if the value in the Index is Nifty 50.

So If any rows is from Nifty 50 but Rising is False, then the Nifty 50 cell should be in Green background of that Only cell and all of the other cells should be in Red.

This is what I want the Dataframe to look like: Whole Row is Red if Rising is False or 0. Color of Index is changed based on whether it is from Nifty-50/100/200

enter image description here

And the code for changing color is as follows:

def highlight_falling(s, column:str):
        '''
        Highlight The rows where average is falling
        args:
            s: Series
            column: Column name(s)
        '''
        is_max = pd.Series(data=False, index=s.index)
        is_max[column] = s.loc[column] == True
        return ['' if is_max.any() else 'background-color: #f7a8a8' for v in is_max]

picked.style.apply(highlight_falling, column=['Rising'], axis=1) # picked is the DF

Here I want to give the Index of 50,100,200,500 cell as [Green,Blue,Magenta. White] (just tan example)



from Recent Questions - Stack Overflow https://ift.tt/2USkDol
https://ift.tt/3BN51Dm

No comments:

Post a Comment