如何在Piechart中执行特定列的总和并基于特定列显示标签?

时间:2019-01-06 02:34:42

标签: python pandas pie-chart

全部

我的数据集如下所示。我试图显示每月总销售额的饼图。下面是我的代码。图表正在显示总量,但是由于某种原因,它仅在图表周围显示1。不知道有什么问题。感谢您提供任何帮助以及解释。

注意:我是Python和matplotlib的新手

数据集

{'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'}}

我的代码:

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

1 个答案:

答案 0 :(得分:0)

要在饼图中显示每月的总销售额。
根据您的数据:
第一个月的总销售量= 10 + 25 + 25 + 5 + 10 = 75,
第12个月的总销售量= 12 + 6 + 4 + 4 + 3 =29。
因此,首先,您需要使用groupby操作计算每月的总销售额,然后再绘制饼图。

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