Using groupby().sum() on a dataframe, then plotting a pie chart with labels?

This is my first question here, I'm quite new to Python/pandas/matplolib

I have this line of code that creates a DataFrame:

repartition = sorted2017.groupby(by=sorted2017["Traitement"]).sum()

It works as I expected, except that the column title "Traitement" seems to appear on its own row:

                  Prix  Coût net  Manuvie     CCQ  SSQ
Traitement                                            
masso (Véro)    213.86       0.0    144.0   69.86  0.0
ostéo (Véro)     80.00       0.0     64.0   16.00  0.0
physio (Danny)  415.00       0.0    265.0  150.00  0.0
physio (Véro)   269.00       0.0    204.8   64.20  0.0
psy (Simone)    500.00       0.0    150.0  350.00  0.0
psy (Véro)      300.00       0.0    240.0   60.00  0.0

I wanted to use the "Traitement" column as labels for my matplotlib pie chart, so I tried :

plt.pie(repartition["Prix"], labels=repartition["Traitement"])

plt.show()

But I get a KeyError. I've also tried with iloc for the labels, but then I get

ValueError : "'label' must be of length 'x'"

How can I fix this?

Upvotes: 2

Views: 5090

Answers (1)

gyoza
gyoza

Reputation: 2152

After groupby, "Traitement" column is in index column.

plt.pie(x=repartition["Prix"], labels=repartition.index)
plt.show()

enter image description here

Upvotes: 3

Related Questions