简体   繁体   中英

Dynamically highlight rows that do not meet threshold in Python

I have a dataset, df, where I would like to essentially highlight rows that do not meet the threshold. (Anywhere the result == 'Too High' | 'Too Low')

 type      start           end            Percent           Result 
  
    A          2019-04-01      2019-05-01     21                A Too High   
    A          2019-05-01      2019-06-01      8                A Ok
    A          2019-07-01      2019-08-01      5                A Too Low                 
    B          2020-06-01      2020-07-01      7                B Too High                               
    B          2020-07-01      2020-08-01      5                B Ok
    B          2020-09-01      2020-10-01      3                B Ok  

My desired outcome:

 To have the 'Too High' rows highlighted in Yellow 
 To have the 'Too Low' rows highlighted in Green

在此处输入图片说明

This is what I am doing

import plotly
import plotly.graph_objs as go
import numpy as np

import pandas as pd

Result = df.Result.values
Too High =
Too Low  =

trace = go.Table(columnwidth = [.3,.3,.3,.3],
    header=dict(values=['Result'],
                fill = dict(color='#FFFF00'),
                font = dict(color = 'white', size = 12),
                align = ['left'] ),
    cells=dict(values=['Too High'],
               fill = dict(color=[#unique color for column
                                                ['FFFF00' if val == 'Too High' else '#7CFC00' for val in result] ]),
               align = ['center'] ))

data = [trace]

I am still researching this. Any suggestion is appreciated

Check out the Styling section of the docs for more usage.

https://pandas.pydata.org/pandas-docs/stable/user_guide/style.html

import pandas as pd

df = pd.read_clipboard(sep='\s\s+')

def highlight(val):

    if 'Too High' in str(val):
        color='yellow'
    elif 'Too Low' in str(val):
        color='green'
    else:
        color=''
    return f'background-color: {color}'

df.style.applymap(highlight)

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM