Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to create a pie-chart from pandas DataFrame?

I have a dataframe, with Count arranged in decending order, that looks something like this:

df = pd.DataFrame({'Topic': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M'],
                   'Count': [80, 75, 70, 65, 60, 55, 50, 45, 40, 35, 30, 25, 20]})

But with more than 50 rows.

I would like to create a pie chart for the top 10 topics and rest of them to be summed up and represent its percentange as label "Others" in the pie chart. Is it possible to exclude the pie labels against each pie, and mention them seperately in a legend?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Thanking in anticipation

>Solution :

Replace Topic by Other if no top N in Series.where and then aggregate sum with Series.plot.pie:

N = 10
df['Topic'] = df['Topic'].where(df['Count'].isin(df['Count'].nlargest(N)), 'Other')

s = df.groupby('Topic')['Count'].sum()

pie = df.plot.pie(y='Count', legend=False)

#https://stackoverflow.com/a/44076433/2901002
labels = [f'{l}, {s:0.1f}%' for l, s in zip(s.index, s / s.sum())]
plt.legend(bbox_to_anchor=(0.85, 1), loc='upper left', labels=labels)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading