python - How to calculate proportion display pie chart in pandas or others?
问题描述
According to this question, I would like to get the proportion of item in total amount, and display a pie chart like below(need to display the name and the proportion of the item):
I use the same dataset as the question:
id date num name desc price
1 7/6/2020 10 pen abcd $1
1 7/6/2020 2 abc efg $3
1 7/6/2020 3 bcd efg $5
2 7/6/2020 3 pen abcd $1
2 7/6/2020 1 pencil abcd $3
2 7/6/2020 2 disk abcd $1
2 7/6/2020 2 paper abcd $1
3 7/6/2020 2 ff pag $100
3 7/6/2020 10 water kml $5
4 7/15/2020 5 gg kml $5
4 7/15/2020 10 cofffee oo $5
5 7/15/2020 5 pp oo $4
6 7/15/2020 2 abc efg $3
6 7/15/2020 3 bcd efg $5
6 7/15/2020 4 aa efg $5
6 7/15/2020 5 bb efg $6
7 7/15/2020 1 bag abcd $50
7 7/15/2020 1 box abcd $20
8 7/15/2020 1 pencil abcd $3
8 7/15/2020 2 disk abcd $1
8 7/15/2020 2 paper abcd $1
8 7/15/2020 2 ff hijk $100
9 8/15/2020 10 water kml $5
9 8/15/2020 5 gg kml $5
9 8/15/2020 10 cofffee oo $5
9 8/15/2020 5 pp oo $4
9 8/15/2020 2 abc efg $3
10 8/15/2020 3 bcd efg $5
10 8/15/2020 4 aa efg $5
10 8/15/2020 5 bb efg $6
11 8/15/2020 1 bag abcd $50
11 8/15/2020 1 box abcd $20
I would like to use matplotlib, pycharts and any other packages are fine, My code as below is not correct:
import pandas as pd
import xlrd
import seaborn as sns
import matplotlib.pyplot as plt
df = pd.read_excel ('./orders.xlsx', sheet_name='Sheet1')
df.groupby(by=['name']).sum()
df['price'] = df['price'].replace('$','', regex=True).astype(int)
df['new'] = df['price'].mul(df['num'])
df1 = df.groupby(by=['name'], as_index=False)['new'].sum()
# df1
# df1['new'] = df1.apply(lambda x: x.sum(), axis=1)
# df1.loc['new'] = df1.apply(lambda x: x.sum()).dropna()
# df1
# data = df1.Series(???)
# # print(data)
# from matplotlib.font_manager import FontProperties
# # myfont=FontProperties(fname=r'C:/Windows/Fonts/simhei.ttf',size=14)
# # sns.set(font=myfont.get_name())
# plt.rcParams['figure.figsize'] = (8.0, 6.0)
# lbs= data.index
# explodes=[0.1 if i=='??' else 0 for i in lbs]
# plt.pie(data, explode=explodes,labels=lbs, autopct="%1.1f%%",
# colors=sns.color_palette("muted"),startangle = 90,pctdistance = 0.6,
# textprops={'fontsize':14,'color':'black'})
# plt.axis('equal')
# plt.show()
解决方案
推荐阅读
- html - 滑块上的淡入淡出动画不起作用(仅限 CSS3 和 html5)
- python - 熊猫系列的部分总和
- java - 尝试将我们的驱动程序连接到连接池
- php - 创建 CSV 并强制下载
- r - R:如何将具有多个值列和(几乎没有)不规则坐标的数据框转换为 RasterStack 或 RasterBrick?
- design-patterns - Avro 作为不同语言客户端使用的 http 套接字的序列化协议
- java - 以编程方式将 Eclipse 格式化程序配置文件 XML 导出到 .prefs
- php - 在没有表单的情况下将图像插入数据库提交 AJAX PHP
- lua - LuaRocks LuaSocket 安装失败
- arrays - Scala:将数组转换为 DataFrame 或 RDD