基于列组条件的DataFrame样式 [英] DataFrame Styling based on conditions for groups of columns

查看:82
本文介绍了基于列组条件的DataFrame样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要设置数据框的样式:

I need to style a Dataframe:

df = DataFrame({'A':['Bob','Rob','Dob'],'B':['Bob', 'Rob','Dob'],'C':['Bob','Dob','Dob'],'D':['Ben','Ten','Zen'],'E':['Ben','Ten','Zu']})
df
     A  B    C  D   E
0   Bob Bob Bob Ben Ben
1   Rob Rob Dob Ten Ten
2   Dob Dob Dob Zen Zu

我需要立即比较A,B,C列,以检查它们是否相等,然后将突出显示/颜色应用于不相等的值. 然后,我需要比较D,E列以检查它们是否相等,然后将突出显示/颜色应用于不相等的值

I need to compare columns - A,B, C at once to check if they are equal and then apply a highlight/color to unequal values. Then I need to compare columns D,E to check if they are equal and then apply a highlight/color to unequal values

喜欢:

df[['A','B','C']].eq(df.iloc[:, 0], axis=0)

     A       B       C
0   True    True    True
1   True    True    False
2   True    True    True

我无法将df.style与df的子集一起应用,然后再进行合并.

I am unable to apply df.style with a subset of df and then concat.

@jezrael的回答:

Response to answer by @jezrael:

推荐答案

我认为需要:

def highlight(x):
    c1 = 'background-color: red'
    c2 = '' 
    #define groups of columns for compare by first value of group ->
    #first with A, second with D
    cols = [['A','B','C'], ['D','E']]

    #join all masks together
    m = pd.concat([x[g].eq(x[g[0]], axis=0) for g in cols], axis=1)
    df1 = pd.DataFrame(c2, index=x.index, columns=x.columns)
    df1 = df1.where(m, c1)
    return df1

df.style.apply(highlight, axis=None)

对于多种颜色,可以通过带有用于比较的列的颜色创建字典:

For multiple colors is possible create dictionary by colors with columns for compare:

def highlight(x):
    c = 'background-color: '
    cols = {'red': ['A','B','C'], 'blue':['D','E']}

    m = pd.concat([x[v].eq(x[v[0]], axis=0).applymap({False:c+k, True:''}.get) 
                   for k, v in cols.items()], axis=1)
    return m

替代解决方案:

def highlight(x):
    c = 'background-color: '
    cols = {'red': ['A','B','C'], 'blue':['D','E']}

    df1 = pd.DataFrame(c, index=x.index, columns=x.columns)

    for k, v in cols.items():
        m = x[v].eq(x[v[0]], axis=0).reindex(columns=x.columns, fill_value=True)
        df1 = df1.where(m, c+k)
    return df1    

df.style.apply(highlight, axis=None)

这篇关于基于列组条件的DataFrame样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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