【问题标题】:Correct row coloring with pandas dataframe styler使用 pandas 数据框样式器更正行着色
【发布时间】:2018-10-21 16:25:49
【问题描述】:

如果颜色基于前一行,我会花时间解决着色表行的问题。

在四点系统中存在以下逻辑。如果 0 或 1 点,则行颜色应为红色,如果 3 或 4 点,则行颜色应为绿色,如果 2 点,则颜色应与前一行相同。

我无法确定数据框中的前一行颜色。我用'temp'列解决了它。不幸的是,此列显示在 HTML 表中。

def should_colored(sum_of_points):
    if sum_of_points > 2:
        return True
    elif sum_of_points < 2:
        return False
    else:
        return np.NaN

def determine_coloring_for_row(results):
    tmp = pd.DataFrame(results.values, columns=['color_result'])

    tmp['color_result'] = tmp['color_result'].apply(lambda i : should_colored(i))    
    tmp['color_result'].fillna(method='ffill', inplace=True)

    return tmp['color_result'].values

def color_row(row, number_of_columns):
    color = 'green' if row['color_result'] else 'red' 

    return ['background-color: %s' % color] * number_of_columns

df['color_result'] = determine_coloring_for_row(df['sum_of_points'])
df.style.apply(color_row, number_of_columns = len(df.columns), axis=1)

有人知道如何通过使用 style.apply 或隐藏元数据列来解决它吗?

【问题讨论】:

    标签: python html pandas dataframe pandas-styles


    【解决方案1】:

    我认为新列不是必需的,只需要 DataFrame of styles 原始 DataFrame 列和索引并使用 mask 按条件设置行:

    def highlight(x):
        c1 = 'background-color: green'
        c2 = 'background-color: red' 
    
        df1 = pd.DataFrame(c2, index=x.index, columns=x.columns)
        df1 = df1.mask(df['sum_of_points'].map(should_colored).ffill(), c1)
        #print (df1)
        return df1
    
    df.style.apply(highlight, axis=None)
    

    示例

    df = pd.DataFrame({'sum_of_points':[0,1,2,1,2,3,4,1,2,2,4,5,0,1,2],
                       'A':range(15)})
    
    print (df)
         A  sum_of_points
    0    0              0
    1    1              1
    2    2              2
    3    3              1
    4    4              2
    5    5              3
    6    6              4
    7    7              1
    8    8              2
    9    9              2
    10  10              4
    11  11              5
    12  12              0
    13  13              1
    14  14              2
    

    def should_colored(sum_of_points):
        if sum_of_points > 2:
            return True
        elif sum_of_points < 2:
            return False
        else:
            return np.NaN
    
    def highlight(x):
        c1 = 'background-color: green'
        c2 = 'background-color: red' 
    
        df1 = pd.DataFrame(c2, index=x.index, columns=x.columns)
        df1 = df1.mask(x['sum_of_points'].map(should_colored).ffill(), c1)
        return df1
    
    df.style.apply(highlight, axis=None)
    

    【讨论】:

    • 首先感谢@jezrael。我把它带到控制台中运行,但 not 在 Flask 中运行。也许这就是我有这么多问题的原因。 def initialize(): df = pd.DataFrame({'sum_of_points':[0,1,2,1,2,3,4,1,2,2,4,4,0,1,2],'A':range(15)}) df.style.apply(highlight, axis=None 如果我不使用全局数据框,它会是什么样子?
    • 数据来自一个数据库,如果 URL 将被调用,该数据库将首先打开。目前我有 undefined name df 错误,我确定是否可以全局以及何时执行此操作。
    • @Clueless - 我没有使用flask 的经验,所以我不知道:(
    • 我现在把它放到一个单行中,并且该方法可以访问本地数据框df.style.apply(lambda x: pd.DataFrame('background-color: red', index=x.index, columns=x.columns).mask(df['sum_of_points'].map(should_colored).ffill(), 'background-color: green'), axis=None) 甚至我想知道这个数据框的东西应该如何在没有全局变量的情况下工作。
    • 它甚至可以使用 highlight 方法。我不得不将 df 更改为 x df1 = df1.mask(x['sum_of_points'].map(should_colored).ffill(), c1)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-03
    • 2017-07-23
    • 1970-01-01
    • 2016-11-25
    • 2019-11-27
    相关资源
    最近更新 更多