Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

color current value cell dataframe

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,

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading