pandas 如何设置多索引 Dataframe 的行的样式?

sg2wtvxw  于 2022-11-27  发布在  其他
关注(0)|答案(1)|浏览(173)

我有以下 Dataframe :

dic = {'US':{'Traffic':{'new':1415, 'repeat':670}, 'Sales':{'new':67068, 'repeat':105677}},
      'UK': {'Traffic':{'new':230, 'repeat':156}, 'Sales':{'new':4568, 'repeat':10738}}}
d1 = defaultdict(dict)
for k, v in dic.items():
    for k1, v1 in v.items():
        for k2, v2 in v1.items():
            d1[(k, k2)].update({k1: v2})

df.insert(loc=2, column=' ', value=None)
df.insert(loc=0, column='Mode', value='Website')
df.columns = df.columns.rename("Metric", level=1)

它看起来像:

我需要使用以下函数中的条件将字体和背景色应用于数据框的流量和销售行的帮助:

def sales_color(val):
    font_color = ''
    background_color = ''
    
    if val <= 10000:
        font_color = 'red'
        background_color = 'light red'
    elif val >= 100000:
        font_color = 'green'
    else:
        font_color = 'grey'
        
    return [font_color, background_color]

def traffic_color(val):
    font_color = 'orange' if val < 300 else 'black'
    background_color = 'light orange' if val < 300 else ''
    return [font_color, background_color]

我尝试了一种效率低下的方法-将颜色单独应用于单元格,但这不起作用:

df['US']['new']['Sales'].style.apply(sales_color)
df['US']['new']['Traffic'].style.apply(traffic_color)
df['US']['Repeat']['Sales'].style.apply(sales_color)
df['US']['Repeat']['Traffic'].style.apply(traffic_color)

df['UK']['new']['Sales'].style.apply(sales_color)
df['UK']['new']['Traffic'].style.apply(traffic_color)
df['UK']['Repeat']['Sales'].style.apply(sales_color)
df['UK']['Repeat']['Traffic'].style.apply(traffic_color)
2izufjch

2izufjch1#

使用自定义函数,通过DataFrame.loc选择,然后通过numpy.wherenumpy.select按条件设置值。
对于不使用light redlight orange颜色的我,我使用colors hex codes代替:

def color(x):

    idx = pd.IndexSlice

    t = x.loc['Traffic', idx[:, ['new','repeat']]]
    s = x.loc['Sales', idx[:, ['new','repeat']]]
    
    df1 = pd.DataFrame('', index=x.index, columns=x.columns)
    
    s1 = np.select([s <= 10000, s >= 100000],  ['background-color: #fa8072; color: red',
                                                 'color: green'], 
                                              default='color: grey')
    t1 = np.where(t <= 300, 'background-color: #ffcc99; color: orange',
                                                       'color: black')
    df1.loc['Sales', idx[:, ['new','repeat']]] = s1
    df1.loc['Traffic', idx[:, ['new','repeat']]] = t1
                                                      
    return df1

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

相关问题