如何将两个以上的格式化条件添加到数据框中的不同行? [英] How to add more than two formatting conditions to different row in a dataframe?

查看:42
本文介绍了如何将两个以上的格式化条件添加到数据框中的不同行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我问了这个问题

我要突出显示彩色行中的最大值,但我也想在其他行中添加其他条件.例如"FORWARD P/E"行中的最小值

所以我的代码如下:

  def highlight_min(s):尝试:is_min = s == s.min()返回['background-color:green',如果在is_min中为v表示v否则为'"除了TypeError:经过除了KeyError:经过def Highlight_max(s):尝试:is_max = s == s.max()返回['background-color:green',如果在is_max中v等于v否则为'除了TypeError:经过除了KeyError:经过df.style.apply(highlight_max,轴= 1,子集=([['每股账面价值(mrq)','稀释每股收益(ttm)','EBITDA','毛利润(ttm)'],df.columns)) 

我的问题是如何在同一数据集中的其他行中使用这些其他条件公式!

非常感谢:)

解决方案

如果为所需的每个函数连接APPLY函数,则可以执行多个过程.我没有数据,所以我得到了APL的损益表并进行了调整.我将最小值更改为红色.

I asked this question How to apply different conditional format to different rows? that was kindly answered by @davmos

Now I have a new problem, that is add more than two condtions in different datarows!

So I have the following dataframe:

Where I am highlighting the max values in the coloured rows, but i also want to add other conditions in other rows. For example the minimum value in the 'FORWARD P/E' row

So my code is the following :

def highlight_min(s):
try:
    is_min = s == s.min()
    return ['background-color: green' if v else '' for v in is_min]
except TypeError:
    pass
except KeyError:
    pass

def highlight_max(s):
    try:
        is_max = s == s.max()
        return ['background-color: green' if v else '' for v in is_max]
    except TypeError:
        pass
    except KeyError:
        pass



df.style.apply(highlight_max, axis=1, subset=(['Book Value Per Share (mrq)', 'Diluted EPS (ttm)','EBITDA', 'Gross Profit (ttm)'], df.columns))

My question is how I can use those other conditional formulas in other rows in the same dataset!

Thank you very much :)

解决方案

If you concatenate the APPLY function for each function you want, you can do multiple processes. I didn't have the data, so I got AAPL's income statement and adapted it. I changed the minimum value to red.refer to pandas style

(df.style.apply(highlight_max, axis=1, subset=(['Research Development', 'Operating Income','Cost Of Revenue'], df.columns))
.apply(highlight_min, axis=1, subset=(['Research Development', 'Operating Income','Cost Of Revenue'], df.columns)))

这篇关于如何将两个以上的格式化条件添加到数据框中的不同行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆