color current value cell dataframe

Question:

I’m looking how I can color in red the current value cell when this value is above the 3sigma+ value please ? I did not succeed with the df.apply() method… If anyone have a simple method, this is welcome.
I want to color only the current value cell based on the value of avg cell.

For example, for the first column, the current value is 1, and the 3sigma+ is 4, this is resulting to color the current value in green background because 1 < 4

Best regards,

import pandas as pd

test = pd.DataFrame({"index" : ['avg', 'min', 'max', '3sigma+', '3sigma-', 'current value'], 
     "A": [1,2,3,4,1,1], "B":[5,3,2,1,1,5],"C":[8,7,0,1,1,6]})
test.set_index("index", inplace = True)

def color(score):
    return f"background-color:" + (" red;" if score < 4 else "green") 

test.style.applymap(color)

result in image here

Asked By: notafk

||

Answers:

Try subset argument

def color(score):
    return [f"background-color:" + (" red;" if score.item() < test.loc['3sigma+', score.name] else "green")]

s = test.style.apply(color, subset=('current value', slice(None)))

s.to_html('74143101.html')

enter image description here

Answered By: Ynjxsjmh
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.