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 pie chart in pandas over different ranges of values from single column?

For example i have this following data

City           Population

abc            5000
mno            2000
xyz            7000
uvw            9000
def            11000

I need pie chart for this with three categoies , underpopulated where population < 5000, moderate-populated where 5000 < population < 10000, and overpopulated where population > 10000.

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

>Solution :

using pd.cut, create the bins, then use groupby to group population and finally draw the pie

labels = ['under','moderate','over']
df['grp'] = pd.cut(df['Population'],
                   (0,5000,10000,100000) , 
                   labels=labels)

plt.pie(df.groupby(['grp'])['Population'].sum(), labels=labels)

enter image description here

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