Home > Blockchain >  How to create pie chart in pandas over different ranges of values from single column?
How to create pie chart in pandas over different ranges of values from single column?

Time:06-16

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.

CodePudding user response:

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

CodePudding user response:

If I understand your question correctly, you're looking for

import pandas as pd
df = pd.DataFrame({'City':['abc', 'mno', 'xyz', 'uvw', 'def'], 'Population':[5000, 2000, 7000, 9000, 11000]})
pd.cut(df.Population, [0, 5000, 10000, 99999]).value_counts().plot.pie()

enter image description here

  • Related