如何根据每个组为数据框着色?

2024-05-16 22:10:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个如下的数据帧

import pandas as pd
import seaborn as sns
import numpy as np

df = sns.load_dataset("diamonds")

df.head()
    carat   cut color   clarity depth   table   price   x   y   z
0   0.23    Ideal   E   SI2 61.5    55.0    326 3.95    3.98    2.43
1   0.21    Premium E   SI1 59.8    61.0    326 3.89    3.84    2.31
2   0.23    Good    E   VS1 56.9    65.0    327 4.05    4.07    2.31
3   0.29    Premium I   VS2 62.4    58.0    334 4.20    4.23    2.63
4   0.31    Good    J   SI2 63.3    58.0    335 4.34    4.35    2.75
cardinal_30 = ['cut', 'color', 'clarity']
# Value Count & Percentage for low carinality columns
c = df[cardinal_30].apply(lambda x: x.value_counts()).T.stack().astype(int)
p = (df[cardinal_30].apply(lambda x: x.value_counts(normalize=True)).T.stack() * 100).round(2)
cp = pd.concat([c,p], axis=1, keys=['Count', 'Percentage %'])

# Rest index and name the axis
cp = cp.rename_axis(['Variable','Class']).reset_index()
cp['Variable'] = np.where(cp['Variable'].duplicated(),'',cp['Variable'])

cp
Variable    Class   Count   Percentage %
0   cut Fair    1610    2.98
1       Good    4906    9.10
2       Ideal   21551   39.95
3       Premium 13791   25.57
4       Very Good   12082   22.40
5   color   D   6775    12.56
6       E   9797    18.16
7       F   9542    17.69
8       G   11292   20.93
9       H   8304    15.39
10      I   5422    10.05
11      J   2808    5.21
12  clarity I1  741 1.37
13      IF  1790    3.32
14      SI1 13065   24.22
15      SI2 9194    17.04
16      VS1 8171    15.15
17      VS2 12258   22.73
18      VVS1    3655    6.78
19      VVS2    5066    9.39

我想为每个变量的数据框为所有列添加不同的颜色

enter image description here

在上图中,所选变量应为一种颜色,下面的变量类型应为另一种颜色等

如何为每个变量组的数据帧添加不同颜色的颜色


Tags: 数据importdf颜色ascountvariablecp
1条回答
网友
1楼 · 发布于 2024-05-16 22:10:26

您可以尝试以下函数,该函数采用matplotlib颜色,并根据变量列将其映射回:

from matplotlib import colors
def colr(x):
    y = x.assign(k=x['Variable'].ne("").cumsum())
    d = dict(enumerate(colors.cnames))
    y[:] = np.broadcast_to(y['k'].map(d).radd('background-color:').to_numpy()[:,None]
                          ,y.shape)
    return y.drop("k",1)
cp.style.apply(colr,axis=None)

enter image description here

相关问题 更多 >