【问题标题】:Python Pandas style highlight specific cells for each column with different conditionPython Pandas 样式突出显示具有不同条件的每一列的特定单元格
【发布时间】:2021-09-24 01:51:17
【问题描述】:

我正在尝试突出显示具有不同条件的每一列的特定单元格,它们的值与每一行的条件匹配。

下图是我想要实现的: The table I attempt to achieve

我搜索了 google 和 stackoverflow,但这些都不能满足我的要求。任何熟悉 Pandas Style 的人都可以提供帮助吗?

以下是我尝试过但失败的代码:

Ex1

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

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

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)

【问题讨论】:

  • 只有3列?
  • 在我的真实案例中,有很多列。我刚刚在这里发布了一个简化的示例。您的第二个答案也符合我的要求。欣赏!

标签: python pandas dataframe styles highlight


【解决方案1】:

如果有相同数量的条件,比如一些列使用:

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)

如果有很多列并且只需要处理其中的一部分:

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)

编辑:

如果需要指定所有掩码但在最后一步过滤器中仅使用某些列:

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

或者去掉不需要的掩码:

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)

【讨论】:

  • 哇,非常感谢@jezrael 的及时回复。这正是我想要的!我在这个问题上花了几天时间,你确实节省了我很多时间。我可以再问一个问题。如果我排除 B 列的条件(大于 2)并且我只想显示与 A 列和 C 列的条件匹配的行(分别大于 6 和小于 3)。例如,在我的例子中,它只会显示索引为 0 和 1 的两行,就像这里的图像link。如何在我的代码中添加过滤器?
  • @KelvinLo - 所以这意味着删除df1.loc[(x['B'] &gt; 2), 'B'] = c1 ?
  • 是的,删除 B 列的条件,但不显示第三行(索引 2)。仅显示突出显示数字的行。就像这张图片link
  • @KelvinLo - 不是underatand,需要删除B列的样式吗?图片中都是灰色的,有必要吗?
  • 我为灰色的图片道歉,重新上传新的。 link。如果我不将条件应用于 B 列,并且我只想显示与 A 列和 C 列的条件匹配的行。
猜你喜欢
  • 1970-01-01
  • 2018-05-02
  • 1970-01-01
  • 1970-01-01
  • 2011-05-12
  • 1970-01-01
  • 1970-01-01
  • 2017-05-08
  • 1970-01-01
相关资源
最近更新 更多