Skip to content
Advertisement

How to color dataframe based on each group?

I have a dataframe as below

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

I want to color the dataframe for each Variable a different color for all columns.

enter image description here

In the above figure, the selected variables should have one color, the below variable type should in another color etc.,

How can I color the dataframe in different colors for each Variable groups?

Advertisement

Answer

You can try the below function which takes matplotlib colours and maps it back based on the Variable column:

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

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement