Apply background color style to rows in DataFrame in a groupby

Question:

Assume I have the following dataframe

iris = pd.read_csv('https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv')

which looks like this

     sepal_length  sepal_width  petal_length  petal_width     species
0             5.1          3.5           1.4          0.2      setosa
1             4.9          3.0           1.4          0.2      setosa
2             4.7          3.2           1.3          0.2      setosa
3             4.6          3.1           1.5          0.2      setosa
4             5.0          3.6           1.4          0.2      setosa
5             5.4          3.9           1.7          0.4      setosa
6             4.6          3.4           1.4          0.3      setosa
7             5.0          3.4           1.5          0.2      setosa
8             4.4          2.9           1.4          0.2      setosa
9             4.9          3.1           1.5          0.1      setosa
10            5.4          3.7           1.5          0.2      setosa
11            4.8          3.4           1.6          0.2      setosa
12            4.8          3.0           1.4          0.1      setosa
13            4.3          3.0           1.1          0.1      setosa
14            5.8          4.0           1.2          0.2      setosa
15            5.7          4.4           1.5          0.4      setosa
16            5.4          3.9           1.3          0.4      setosa
17            5.1          3.5           1.4          0.3      setosa
18            5.7          3.8           1.7          0.3      setosa
19            5.1          3.8           1.5          0.3      setosa
20            5.4          3.4           1.7          0.2      setosa
21            5.1          3.7           1.5          0.4      setosa
22            4.6          3.6           1.0          0.2      setosa
23            5.1          3.3           1.7          0.5      setosa
24            4.8          3.4           1.9          0.2      setosa
25            5.0          3.0           1.6          0.2      setosa
26            5.0          3.4           1.6          0.4      setosa
27            5.2          3.5           1.5          0.2      setosa
28            5.2          3.4           1.4          0.2      setosa
29            4.7          3.2           1.6          0.2      setosa
30            4.8          3.1           1.6          0.2      setosa
31            5.4          3.4           1.5          0.4      setosa
32            5.2          4.1           1.5          0.1      setosa
33            5.5          4.2           1.4          0.2      setosa
34            4.9          3.1           1.5          0.2      setosa
35            5.0          3.2           1.2          0.2      setosa
36            5.5          3.5           1.3          0.2      setosa
37            4.9          3.6           1.4          0.1      setosa
38            4.4          3.0           1.3          0.2      setosa
39            5.1          3.4           1.5          0.2      setosa
40            5.0          3.5           1.3          0.3      setosa
41            4.5          2.3           1.3          0.3      setosa
42            4.4          3.2           1.3          0.2      setosa
43            5.0          3.5           1.6          0.6      setosa
44            5.1          3.8           1.9          0.4      setosa
45            4.8          3.0           1.4          0.3      setosa
46            5.1          3.8           1.6          0.2      setosa
47            4.6          3.2           1.4          0.2      setosa
48            5.3          3.7           1.5          0.2      setosa
49            5.0          3.3           1.4          0.2      setosa
50            7.0          3.2           4.7          1.4  versicolor
51            6.4          3.2           4.5          1.5  versicolor
52            6.9          3.1           4.9          1.5  versicolor
53            5.5          2.3           4.0          1.3  versicolor
54            6.5          2.8           4.6          1.5  versicolor
55            5.7          2.8           4.5          1.3  versicolor
56            6.3          3.3           4.7          1.6  versicolor
57            4.9          2.4           3.3          1.0  versicolor
58            6.6          2.9           4.6          1.3  versicolor
59            5.2          2.7           3.9          1.4  versicolor
60            5.0          2.0           3.5          1.0  versicolor
61            5.9          3.0           4.2          1.5  versicolor
62            6.0          2.2           4.0          1.0  versicolor
63            6.1          2.9           4.7          1.4  versicolor
64            5.6          2.9           3.6          1.3  versicolor
65            6.7          3.1           4.4          1.4  versicolor
66            5.6          3.0           4.5          1.5  versicolor
67            5.8          2.7           4.1          1.0  versicolor
68            6.2          2.2           4.5          1.5  versicolor
69            5.6          2.5           3.9          1.1  versicolor
70            5.9          3.2           4.8          1.8  versicolor
71            6.1          2.8           4.0          1.3  versicolor
72            6.3          2.5           4.9          1.5  versicolor
73            6.1          2.8           4.7          1.2  versicolor
74            6.4          2.9           4.3          1.3  versicolor
75            6.6          3.0           4.4          1.4  versicolor
76            6.8          2.8           4.8          1.4  versicolor
77            6.7          3.0           5.0          1.7  versicolor
78            6.0          2.9           4.5          1.5  versicolor
79            5.7          2.6           3.5          1.0  versicolor
80            5.5          2.4           3.8          1.1  versicolor
81            5.5          2.4           3.7          1.0  versicolor
82            5.8          2.7           3.9          1.2  versicolor
83            6.0          2.7           5.1          1.6  versicolor
84            5.4          3.0           4.5          1.5  versicolor
85            6.0          3.4           4.5          1.6  versicolor
86            6.7          3.1           4.7          1.5  versicolor
87            6.3          2.3           4.4          1.3  versicolor
88            5.6          3.0           4.1          1.3  versicolor
89            5.5          2.5           4.0          1.3  versicolor
90            5.5          2.6           4.4          1.2  versicolor
91            6.1          3.0           4.6          1.4  versicolor
92            5.8          2.6           4.0          1.2  versicolor
93            5.0          2.3           3.3          1.0  versicolor
94            5.6          2.7           4.2          1.3  versicolor
95            5.7          3.0           4.2          1.2  versicolor
96            5.7          2.9           4.2          1.3  versicolor
97            6.2          2.9           4.3          1.3  versicolor
98            5.1          2.5           3.0          1.1  versicolor
99            5.7          2.8           4.1          1.3  versicolor
100           6.3          3.3           6.0          2.5   virginica
101           5.8          2.7           5.1          1.9   virginica
102           7.1          3.0           5.9          2.1   virginica
103           6.3          2.9           5.6          1.8   virginica
104           6.5          3.0           5.8          2.2   virginica
105           7.6          3.0           6.6          2.1   virginica
106           4.9          2.5           4.5          1.7   virginica
107           7.3          2.9           6.3          1.8   virginica
108           6.7          2.5           5.8          1.8   virginica
109           7.2          3.6           6.1          2.5   virginica
110           6.5          3.2           5.1          2.0   virginica
111           6.4          2.7           5.3          1.9   virginica
112           6.8          3.0           5.5          2.1   virginica
113           5.7          2.5           5.0          2.0   virginica
114           5.8          2.8           5.1          2.4   virginica
115           6.4          3.2           5.3          2.3   virginica
116           6.5          3.0           5.5          1.8   virginica
117           7.7          3.8           6.7          2.2   virginica
118           7.7          2.6           6.9          2.3   virginica
119           6.0          2.2           5.0          1.5   virginica
120           6.9          3.2           5.7          2.3   virginica
121           5.6          2.8           4.9          2.0   virginica
122           7.7          2.8           6.7          2.0   virginica
123           6.3          2.7           4.9          1.8   virginica
124           6.7          3.3           5.7          2.1   virginica
125           7.2          3.2           6.0          1.8   virginica
126           6.2          2.8           4.8          1.8   virginica
127           6.1          3.0           4.9          1.8   virginica
128           6.4          2.8           5.6          2.1   virginica
129           7.2          3.0           5.8          1.6   virginica
130           7.4          2.8           6.1          1.9   virginica
131           7.9          3.8           6.4          2.0   virginica
132           6.4          2.8           5.6          2.2   virginica
133           6.3          2.8           5.1          1.5   virginica
134           6.1          2.6           5.6          1.4   virginica
135           7.7          3.0           6.1          2.3   virginica
136           6.3          3.4           5.6          2.4   virginica
137           6.4          3.1           5.5          1.8   virginica
138           6.0          3.0           4.8          1.8   virginica
139           6.9          3.1           5.4          2.1   virginica
140           6.7          3.1           5.6          2.4   virginica
141           6.9          3.1           5.1          2.3   virginica
142           5.8          2.7           5.1          1.9   virginica
143           6.8          3.2           5.9          2.3   virginica
144           6.7          3.3           5.7          2.5   virginica
145           6.7          3.0           5.2          2.3   virginica
146           6.3          2.5           5.0          1.9   virginica
147           6.5          3.0           5.2          2.0   virginica
148           6.2          3.4           5.4          2.3   virginica
149           5.9          3.0           5.1          1.8   virginica

I would like to write this to an excel file where I add a background color based on the value of the species columns. I want to highlight the entire row, not just the species column. I want to cycle between two colors, such that setosa is red, versicolor is blue, virginica is red, and so on. In general I do not know how many groups I have, so it must be general enough to account for this

How would I go about achieving this? The df.style.apply works in individual rows. I thought I could do a groupby and then apply the color to all rows within the group, but I was not able to "combine" the formatted groups into one dataframe.

Asked By: Yoda

||

Answers:

If need coloring all rows/ columns use Styler.apply with custom function with repeat mapped values by numpy.broadcast_to, for colors is used seaborne.color_palette:

import seaborn as sns


def color(x):

    vals = x['species'].drop_duplicates()
    palette = sns.color_palette(None, len(vals)).as_hex()
    d = {x: f'background-color:{y}' for x, y in zip(vals, palette)}

    a = np.broadcast_to(x['species'].map(d).fillna('').to_numpy()[:, None], x.shape)
    return pd.DataFrame(a, index=x.index, columns=x.columns)

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

If need coloring only column species use Styler.applymap with lambda function and dict.get:

vals = iris['species'].drop_duplicates()
palette = sns.color_palette(None, len(vals)).as_hex()
d = {x: f'background-color:{y}' for x, y in zip(vals, palette)}

iris.style.applymap(lambda x: d.get(x,''), subset=['species'])
Answered By: jezrael

You can do something like:

def color_species(val):
    color = "red" if val == "setosa" else "blue" if val == "versicolor" else "green"
    return "background-color: %s" % color


iris.style.applymap(color_species, subset=["species"])
Answered By: Faisal Nazik

I see this as having two classes of rows: those that correspond to even and odd groups of rows. To accomplish this you can group by the column you want (df.groupby(...)), calculate the group numbers (.ngroup()) and classify the groups as even and odd (... % 2).

Although it is not needed for your particular example, you then need to sort by the group by column to make sure that all the rows belonging to the same group are contiguous. Otherwise you might get adjacent rows from different groups with the same color.

import pandas as pd
import seaborn as sns

# Load the dataset
df = sns.load_dataset("iris")

# Choose the group keys
groupby_col = "species"
group_keys = df.groupby(groupby_col).ngroup() % 2
nunique, values = group_keys.nunique(), group_keys.unique()

# Create a array of colors in hex format
colors = sns.color_palette(n_colors=nunique).as_hex()

# Map the colors in the array to the groups
color_map = {k: f"background-color: { v }" for k, v in zip(values, colors)}

# Map the colors to the rows
mapping = group_keys.map(color_map)

# The function just returns the previous series
df.sort_values(groupby_col).style.apply(lambda x: mapping)

The preceding code can be easily modified to give different colors for each group instead of alternating colors.

Answered By: dicristina
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.