0

I want to plot a pie chart from an categorical variable. I tried the following ways:

#example df

data = {'Category':  ['waschen','anziehen','lesen', 'waschen', 'waschen', 'lesen', 'lesen'],
        'T1': ['1', '2', '3','3', '5', '6', '7']}
df = pd.DataFrame(data)

#change dtype, doesn't work without

df.loc[:, 'T1']=df.loc[:, 'T1'].astype('int64')

1st try

df.groupby(by=['Category']).sum().plot.pie(y='T1', autopct='%2f');

pie chart is showing the wrong values, for "lesen" und "waschen" there must be the same value

2nd try

df.groupby('Category').count().plot.pie(y='T1', ax=ax, autopct='%2f')

only shows the following text: AxesSubplot:ylabel='T1'

I'm beginner with python and searched the last hours for a solution. Has anybody an idea?

pie1

BigBen
  • 46,229
  • 7
  • 24
  • 40
Kajette
  • 47
  • 4
  • It's plotting the image I have uploaded (I hope you can see it?). I want was Python to show with count() how often each of the three categories occur. I'm sorry, I'm sure it is a "beginner problem". – Kajette Apr 13 '23 at 20:36
  • Not at all. But thank you. – Kajette Apr 13 '23 at 20:50
  • `values=df['Category'].value_counts()`, `plt.pie(values, autopct= lambda x: '{:.0f}'.format(x*values.sum()/100))`, `plt.show()` – BigBen Apr 13 '23 at 20:51
  • But I found this right now: https://stackoverflow.com/questions/62265209/group-data-by-name-count-it-and-make-a-pie-chart That seems to answer my question. – Kajette Apr 13 '23 at 20:51
  • Do you want percentages, or counts? – BigBen Apr 13 '23 at 20:52
  • Both is fine. All I want that they are correct. I would try your last suggestion. Thx. – Kajette Apr 13 '23 at 21:04

0 Answers0