pandas 将背景颜色样式应用于groupby中DataFrame中的行

aiazj4mn  于 2022-12-16  发布在  其他
关注(0)|答案(3)|浏览(192)

假设我有以下 Dataframe

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

看起来像这样

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

我想将此内容写入excel文件,在其中根据species列的值添加背景色。**我想突出显示整行,而不仅仅是species列。**我想在两种颜色之间循环,例如setosa为红色,versicolor为蓝色,virginica为红色,等等。一般来说,我不知道我有多少个群,所以它必须足够一般才能说明这一点
我该如何着手实现这一点呢?df.style.apply在单独的行中工作。我以为我可以做一个groupby,然后将颜色应用到组中的所有行,但我无法将格式化的组“合并”到一个 Dataframe 中。

wnavrhmk

wnavrhmk1#

如果需要对所有行/列进行着色,则使用Styler.apply和自定义函数,并通过numpy.broadcast_to重复Map值,对于颜色,使用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)

如果只需要着色列species,则使用带lambda函数的Styler.applymapdict.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'])
wgeznvg7

wgeznvg72#

您可以执行以下操作:

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"])
xdyibdwo

xdyibdwo3#

我将其视为两类行:要实现这一点,您可以按所需的列分组(df.groupby(...)),计算组号(.ngroup()),并将组分为偶数组和奇数组(... % 2)。
虽然对于您的特定示例来说不需要这样做,但您需要按列按组排序,以确保属于同一组的所有行都是连续的。否则,您可能会得到来自不同组的具有相同颜色的相邻行。

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)

可以很容易地修改前面的代码,为每个组给予不同的颜色,而不是交替颜色。

相关问题