【问题标题】:Python: Color pandas dataframe based on MultiIndexPython:基于 MultiIndex 的彩色熊猫数据框
【发布时间】:2020-05-03 06:25:29
【问题描述】:

考虑这个 MultiIndex 数据帧

i = pd.MultiIndex.from_tuples([(0, 0), (0, 1), (1, 0), (1, 1)], names=['level_0', 'level_1'])
df = pd.DataFrame(range(0, 4), index=i, columns=['foo'])

还有这个颜色字典

colors = {0: (0.6, 0.8, 0.8, 1), 1: (1, 0.9, 0.4, 1)}

其中元组的前三项是 RGB 值,第四项是透明度 alpha。

df如何根据level_0索引着色?

这个结果会很不错:

但这里我会考虑美术:

在后一种样式中,较亮的单元格将具有相同的 RGB 设置,但透明度为 0.25。

【问题讨论】:

    标签: python pandas dataframe colors multi-index


    【解决方案1】:

    您只能使用Styler.set_table_styles 设置样式MultiIndex 的第一级:

    i = pd.MultiIndex.from_tuples([(0, 0), (0, 1), (1, 0), (1, 1)], names=['level_0', 'level_1'])
    df = pd.DataFrame(range(0, 4), index=i, columns=['foo']) 
    #print (df)
    
    import matplotlib.colors as col
    colors = {0: (0.6, 0.8, 0.8, 1), 1: (1, 0.9, 0.4, 1)}
    
    c = {k:col.rgb2hex(v) for k, v in colors.items()}
    idx = df.index.get_level_values(0)
    
    css = [{'selector': f'.row{i}.level0','props': [('background-color', c[v])]}
                 for i,v in enumerate(idx)]
    print (css)
    
    df.style.set_table_styles(css)
    

    第二个也是可以的,但更复杂:

    colors = {0: (0.6, 0.8, 0.8, 1), 1: (1, 0.9, 0.4, 1)}
    #converts grba to integers
    c1 = {k: (int(r * 255),int(g * 255),int(b * 255), a) for k, (r,g,b,a) in colors.items()}
    c2 = {k: (int(r * 255),int(g * 255),int(b * 255), 0.25) for k, (r,g,b,a) in colors.items()}
    
    #get values of first level of MulitIndex
    idx = df.index.get_level_values(0)
    #set css for first level
    css = [{'selector': f'.row{i}.level0', 
            'props': [('background-color', f'rgba{c1[j]}')]} for i,j in enumerate(idx)]
    #counter per first level for pair and unpair coloring
    zipped = zip(df.groupby(idx).cumcount(), enumerate(idx))
    
    css1 = [{'selector': f'.row{i}', 'props': [('background-color', f'rgba{c1[j]}')]} 
           if v % 2 == 0 
           else {'selector': f'.row{i}', 'props': [('background-color', f'rgba{c2[j]}')]} 
           for v,(i, j) in zipped]
    
    
    df.style.set_table_styles(css1 + css)
    

    【讨论】:

    • 太棒了!谢谢@jezrael,你已经完成了艺术。注意:要显示,我只调用df.style.set_table_styles(css1 + css)。
    • @hyco - 是的,这是一个非常有趣的问题,但真的很难,这是一个挑战;)
    • 见下文css甚至没有必要。
    【解决方案2】:

    @jezrael 完成了艺术!

    import pandas as pd
    
    i = pd.MultiIndex.from_tuples([(0, 'zero'), (0, 'one'), (0, 'two'), (1, 'zero'), (1, 'one')], names=['level_0', 'level_1'])
    df = pd.DataFrame(range(0, 5), index=i, columns=['foo']) 
    
    colors = {0: (0.6, 0.8, 0.8, 1), 1: (1, 0.9, 0.4, 1)}
    #convert rgba to integers
    c1 = {k: (int(r * 255),int(g * 255),int(b * 255), a) for k, (r,g,b,a) in colors.items()}
    c2 = {k: (int(r * 255),int(g * 255),int(b * 255), 0.25) for k, (r,g,b,a) in colors.items()}
    
    #get values of first level of MulitIndex
    idx = df.index.get_level_values(0)
    #counter per first level for pair and unpair coloring
    zipped = zip(df.groupby(idx).cumcount(), enumerate(idx))
    
    css = [{'selector': f'.row{i}', 'props': [('background-color', f'rgba{c1[j]}')]} 
           if v % 2 == 0 
           else {'selector': f'.row{i}', 'props': [('background-color', f'rgba{c2[j]}')]} 
           for v,(i, j) in zipped]
    
    df.style.set_table_styles(css)
    

    与@jezrael 的解决方案相比,上面的更短。

    我也喜欢该解决方案不依赖于索引标签是有序整数。请注意,我使用的是字符串(至少对于第二个索引级别)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-16
      • 1970-01-01
      • 2023-01-12
      • 2021-12-30
      • 1970-01-01
      • 2019-03-19
      • 1970-01-01
      相关资源
      最近更新 更多