简体   繁体   中英

How to perform sum of specific column and display labels based on specific column in Piechart?

All,

My dataset looks like following. I am trying to display pie chart for total sales per month. Below is my code. The chart is diplaying total quantity however, for some reason it is only displaying 1 around chart. Not sure what could be wrong. Any help along with explanation is appreciated.

Note: I am new to Python and matplotlib

DataSet

{'InvoiceNo': {42479: '539993', 42480: '539993', 42481: '539993', 42482: '539993', 42483: '539993', 541869: '581587', 541870: '581587', 541871: '581587', 541872: '581587', 541873: '581587'}, 'StockCode': {42479: '22386', 42480: '21499', 42481: '21498', 42482: '22379', 42483: '20718', 541869: '22613', 541870: '22899', 541871: '23254', 541872: '23255', 541873: '22138'}, 'Description': {42479: 'JUMBO BAG PINK POLKADOT', 42480: 'BLUE POLKADOT WRAP', 42481: 'RED RETROSPOT WRAP ', 42482: 'RECYCLING BAG RETROSPOT ', 42483: 'RED RETROSPOT SHOPPER BAG', 541869: 'PACK OF 20 SPACEBOY NAPKINS', 541870: "CHILDREN'S APRON DOLLY GIRL ", 541871: 'CHILDRENS CUTLERY DOLLY GIRL ', 541872: 'CHILDRENS CUTLERY CIRCUS PARADE', 541873: 'BAKING SET 9 PIECE RETROSPOT '}, 'Quantity': {42479: 10, 42480: 25, 42481: 25, 42482: 5, 42483: 10, 541869: 12, 541870: 6, 541871: 4, 541872: 4, 541873: 3}, 'UnitPrice': {42479: 1.95, 42480: 0.42, 42481: 0.42, 42482: 2.1, 42483: 1.25, 541869: 0.85, 541870: 2.1, 541871: 4.15, 541872: 4.15, 541873: 4.95}, 'Amount': {42479: 19.5, 42480: 10.5, 42481: 10.5, 42482: 10.5, 42483: 12.5, 541869: 10.2, 541870: 12.6, 541871: 16.6, 541872: 16.6, 541873: 14.85}, 'InvoiceDate': {42479: '04-01-11', 42480: '04-01-11', 42481: '04-01-11', 42482: '04-01-11', 42483: '04-01-11', 541869: '09-12-11', 541870: '09-12-11', 541871: '09-12-11', 541872: '09-12-11', 541873: '09-12-11'}, 'Day': {42479: 4, 42480: 4, 42481: 4, 42482: 4, 42483: 4, 541869: 9, 541870: 9, 541871: 9, 541872: 9, 541873: 9}, 'Month': {42479: 1, 42480: 1, 42481: 1, 42482: 1, 42483: 1, 541869: 12, 541870: 12, 541871: 12, 541872: 12, 541873: 12}, 'Year': {42479: 2011, 42480: 2011, 42481: 2011, 42482: 2011, 42483: 2011, 541869: 2011, 541870: 2011, 541871: 2011, 541872: 2011, 541873: 2011}, 'CustomerID': {42479: 13313.0, 42480: 13313.0, 42481: 13313.0, 42482: 13313.0, 42483: 13313.0, 541869: 12680.0, 541870: 12680.0, 541871: 12680.0, 541872: 12680.0, 541873: 12680.0}, 'Country': {42479: 'United Kingdom', 42480: 'United Kingdom', 42481: 'United Kingdom', 42482: 'United Kingdom', 42483: 'United Kingdom', 541869: 'France', 541870: 'France', 541871: 'France', 541872: 'France', 541873: 'France'}}

My Code:

df.Quantity.value_counts().plot.pie(labels=df['Month'])
plt.show()

As you want to show total sales per month in pie chart.
According to your data :
Total sale in 1st month = 10 + 25 + 25 + 5 + 10 = 75 ,
Total sale in 12th month = 12 + 6 + 4 + 4 + 3 = 29 .
So first you need to count total sale per month using groupby operation and after that plot pie chart.

new_df = df.groupby(['Month'])['Quantity'].sum()
new_df.plot.pie(y='Month', figsize=(5, 5))
plt.show()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM