pandas-将实际值与目标值进行比较,并使用样式器[英] pandas - compare actual value against a target with styler

本文是小编为大家收集整理的关于pandas-将实际值与目标值进行比较,并使用样式器的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我有一些数据,其中包含实际值的列,然后包含原始目标值

   dimension | metric_actual | metric_target | metric2_actual | metric2_target
        A          16.41            20.00             68.54%            72.00%
        B          17.39            18.00             63.87%            60.00%

理想情况下,如果metric_actual小于目标,我希望该单元格具有红色文本,如果它大于目标,则具有绿色文本.

我想使用.STYLE选项将数据框架输出到电子邮件中.我似乎无法弄清楚如何行分行执行此操作,尽管目标是否有潜在的不同.

style.html

这些功能起作用,但前提是我有一个静态列.我的问题是,对于数据中每一行的同一指标的目标是不同的.

def color_negative_red(value, target):
    color = 'red' if value < target else 'green'
    return 'color: %s' % color

编辑问题是我得到了"真相是模棱两可的错误":

 df.style.applymap(color_negative_red, target=df['metric_target'], subset=['metric_actual']

我觉得应该这样做,但是我需要将每个行的每个值与同一行中的相应目标值进行比较.

推荐答案

在您提供的参考页面中,它说Styler.applymap是elementwise,Styler.apply是列 -/row-/table-wise.我使用了分别修改color_negative_red函数的列版本.

import pandas as pd

def color_negative_red(value, target):
    is_smaller = value < target  # a pandas.Series of dtype bool
    colors = is_smaller.apply(lambda b: 'color: red' if b else 'color: green')  # a pandas.Series of dtype object (i.e. str)
    return colors

df = pd.DataFrame(data={'metric_actual': [16.41, 17.39, 28.23],
                        'metric_target': [20.00, 18.00, 19.00]})

df.style.apply(color_negative_red, target=df['metric_target'], subset=['metric_actual'], )

给了我这个结果:

在此处输入图像描述

本文地址:https://www.itbaoku.cn/post/2622427.html

问题描述

I have some data which contains columns for the actual value and then the original target value

   dimension | metric_actual | metric_target | metric2_actual | metric2_target
        A          16.41            20.00             68.54%            72.00%
        B          17.39            18.00             63.87%            60.00%

Ideally, I want the cell to have red text if the metric_actual is less than the target, and to have green text if it is greater than the target.

I would like to output a dataframe into an email using the .style options. I can't seem to figure out how to do this on a row by row basis though if the target is potentially different for each row.

http://pandas.pydata.org/pandas-docs/stable/style.html

These functions work but only if I have one static column. My issue is that the targets are different for the same metric for each row in the data.

def color_negative_red(value, target):
    color = 'red' if value < target else 'green'
    return 'color: %s' % color

Edit the issue is that I get 'truth is ambiguous errors':

 df.style.applymap(color_negative_red, target=df['metric_target'], subset=['metric_actual']

I feel like this should be possible but I need to compare each value per row to the corresponding target value in that same row.

推荐答案

In the reference page you provided it says that Styler.applymap is elementwise and Styler.apply is column-/row-/table-wise. I used the column-wise version with respectively modified color_negative_red function.

import pandas as pd

def color_negative_red(value, target):
    is_smaller = value < target  # a pandas.Series of dtype bool
    colors = is_smaller.apply(lambda b: 'color: red' if b else 'color: green')  # a pandas.Series of dtype object (i.e. str)
    return colors

df = pd.DataFrame(data={'metric_actual': [16.41, 17.39, 28.23],
                        'metric_target': [20.00, 18.00, 19.00]})

df.style.apply(color_negative_red, target=df['metric_target'], subset=['metric_actual'], )

Gave me this result:

enter image description here