Home > Software design >  Groupby data in Pandad to label my pie chart
Groupby data in Pandad to label my pie chart

Time:11-12

I have using groupby in Pandas,

df_0 = df_find_quality_und.groupby("user score").size()
df_0

and get this output;

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.

Then, I want to plot this data into a pie-chart. But I don't know how to take the left data only to become the labels of my pie-chart. Hence, I write it manually like this;

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)

The pie-chart was well shown in the output with the label. But, I don't want to write the labels manually every time.. Can anyone help me on how to label the pie-chart by taking the groupby data on the left side?

Thank you.

CodePudding user response:

Pandas allows you to plot pie chart:

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

output:

enter image description here

CodePudding user response:

The labels are actually the index of the groupby sizes object, so, you can try:

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

CodePudding user response:

You should be able to use the .index to get the labels:

Could you please try:

plot = plt.pie(df_find_quality_und.groupby("user score").size(), labels=df_find_quality_und.groupby("user score").size().index())

Or given you have previously defined df_find_quality_und.groupby("user score").size() as df_0:

plot = plt.pie(df_0,df_0.index)
  • Related