DataFrame基于列组条件的样式(DataFrame Styling based on conditions for groups of columns)

编程入门 行业动态 更新时间:2024-10-25 11:23:27
DataFrame基于列组条件的样式(DataFrame Styling based on conditions for groups of columns)

我需要设计一个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来检查它们是否相等,然后将高亮/颜色应用于不等值

喜欢:

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的子集,然后应用concat。

回复@jezrael回应:

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

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

like:

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

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

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)

PIC

编辑:对于多种颜色是可能的创建字典的颜色与列进行比较:

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

PIC2

EDIT1:

替代方案:

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)

I believe need:

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)

pic

EDIT: 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

pic2

EDIT1:

Alternative solution:

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)

更多推荐

本文发布于:2023-08-02 15:24:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1378323.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:样式   条件   Styling   DataFrame   groups

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!