Pandas 中的 Groupby 数据来标记我的饼图

Groupby data in Pandad to label my pie chart

我在 Pandas、

中使用了 groupby
df_0 = df_find_quality_und.groupby("user score").size()
df_0

并得到这个输出;

Output:
user score
3.0    14
7.0     2
7.1     1
7.2     2
7.3     1
7.5     1
7.7     1
7.8     2
7.9     1
8.0     1
8.1     2
8.3     1
8.7     1
dtype: int64

The right side is the frequency of the the data.

然后,我想将这些数据绘制成饼图。但是我不知道如何只把左边的数据变成我饼图的标签。所以,我是这样手动写的;

user_sc_und = [3.0,7.0,7.1,7.2,7.3,7.5,7.7,7.8,7.9,8.0,8.1,8.3,8.7]
user_sc_und_1 = df_find_quality_und.groupby("user score")
plot = plt.pie(df_0, labels=user_sc_und)

饼图很好地显示在带有标签的输出中。但是,我不想每次都手动编写标签。任何人都可以通过在 left 侧获取 groupby 数据来帮助我如何标记饼图吗?

谢谢。

标签实际上是groupby sizes对象的索引,所以,你可以试试:

plot = plt.pie(df_0, labels=df_0.index)

Pandas 允许您绘制饼图:

df_0 = df_find_quality_und.groupby("user score").size()
df_0.plot.pie(autopct="%.2f",figsize=(10,10))

输出: