简体   繁体   English

Python Pandas 样式突出显示具有不同条件的每列的特定单元格

[英]Python Pandas style highlight specific cells for each column with different condition

I'm trying to highlight specific cells for each column with different condition which their value matches the condition for each row.我试图突出显示具有不同条件的每一列的特定单元格,它们的值与每一行的条件匹配。

Below image is what I want to achieve: The table I attempt to achieve下图是我想要实现的:我试图实现的表

I searched google and stackoverflow but none of these can meet my requirement.我搜索了谷歌和stackoverflow,但这些都不能满足我的要求。 Can anyone who's familiar with Pandas Style could assist?任何熟悉 Pandas Style 的人都可以提供帮助吗?

Below are the codes I tried and failed:以下是我尝试并失败的代码:

Ex1防爆1

import pandas as pd
df = pd.DataFrame([[10,3,1], [3,7,2], [2,4,4]], columns=list("ABC"))

def highlight(s):
    return ['background-color: yellow' if (v>2) else 'background-color: white' for v in s]
df.style.apply(highlight, axis=0)

Ex2 Ex2

import pandas as pd
df = pd.DataFrame([[10,3,1], [3,7,2], [2,4,4]], columns=list("ABC"))

Column_limit = (df['A'] > 6) | (df['B'] > 2) | (df['C'] < 3)
df[Column_limit].style.applymap(lambda x: 'background-color: yellow', subset=pd.IndexSlice[:, ['A', 'C']])

Ex3 Ex3

import pandas as pd
df = pd.DataFrame([[10,3,1], [3,7,2], [2,4,4]], columns=list("ABC"))

subsets = pd.IndexSlice[:, 'A']
df.style.applymap(lambda x: 'background-color: yellow', subset = subsets)

If there is same number of conditions like some number of columns use:如果存在相同数量的条件,例如某些列数,请使用:

df = pd.DataFrame([[10,3,1], [3,7,2], [2,4,4]], columns=list("ABC"))

def highlight(x):
    c1 = 'background-color: yellow'

    # condition
    m = pd.concat([(x['A'] > 6), (x['B'] > 2), (x['C'] < 3)], axis=1)
    #print (m)
    #empty DataFrame of styles
    df1 = pd.DataFrame('', index=x.index, columns=x.columns)
    #set new columns by condition
    return df1.mask(m, c1)


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

If there is a lot of columns and need processing only some of them:如果有很多列并且只需要处理其中的一些:

def highlight(x):
    c1 = 'background-color: yellow'

    #empty DataFrame of styles
    df1 = pd.DataFrame('', index=x.index, columns=x.columns)
    #set new columns by condition
    
    df1.loc[(x['A'] > 6), 'A'] = c1
    df1.loc[(x['B'] > 2), 'B'] = c1
    df1.loc[(x['C'] < 3), 'C'] = c1
    
    return df1

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

EDIT:编辑:

If need specified all masks but in last step filter only some columns use:如果需要指定所有掩码,但在最后一步过滤器中仅使用某些列:

def highlight(x):
    c1 = 'background-color: yellow'

    #empty DataFrame of styles
    df1 = pd.DataFrame('', index=x.index, columns=x.columns)
    #set new columns by condition
    
    df1.loc[(x['A'] > 6), 'A'] = c1
    df1.loc[(x['B'] > 2), 'B'] = c1
    df1.loc[(x['C'] < 3), 'C'] = c1
    
    need = ['A','C']
    df1 = df1[need].reindex(x.columns, fill_value='', axis=1)
    return df1

Or remove masks which not necessary:或删除不必要的面具:

def highlight(x):
    c1 = 'background-color: yellow'

    #empty DataFrame of styles
    df1 = pd.DataFrame('', index=x.index, columns=x.columns)
    #set new columns by condition
    
    df1.loc[(x['A'] > 6), 'A'] = c1
    df1.loc[(x['C'] < 3), 'C'] = c1
    
    return df1

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python Pandas样式函数可在特定条件下突出显示特定列 - Python Pandas style function to highlight specific columns under specific condition 如何通过每列的不同条件突出显示 dataframe 中的某些单元格? 是否也可以突出显示系列中的某些数字? - How to highlight some cells in a dataframe through different condition for each column? Is also possible highlighting some number in a Series? Pygsheets:如何根据条件突出显示特定列中的单元格 - Pygsheets: How to highlight cells in a specific column based on a condition 熊猫样式中每列的颜色不同 - Different color for each column in pandas style Python pandas 两列之间的高亮值匹配条件 excel - Python pandas Highlight value match condition between two column excel Python 和 Pandas:如何计算具有特定条件的列 - Python and Pandas: How count a column with specific condition Python:xlsxwriter 在没有条件的情况下按范围突出显示单元格 - Python: xlsxwriter highlight cells by range without condition Pandas 样式 function 突出显示特定列 - Pandas style function to highlight specific columns 突出显示 pandas 中每一行的逐列差异 - highlight column by column difference for each row in pandas pandas:根据不同的算术条件获取每列内的计数 - pandas: get count within each column based on different arithmetic condition
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM