2022 National University Student Data Analysis Competition Question A-Pharmaceutical E-commerce Sales Data Analysis

Novices to participate in the competition, please understand the shortcomings

Topic link: Link: https://pan.baidu.com/s/1ll7Y1kTr1MiJTjErwnqiVA 
Extraction code: zu9f

Table of contents

Task 1 Analyze the store

Task 1.1 Store Type Statistics

Task 1.2 Proportion of store sales

Task 1.3 Sales of Ali Health Pharmacy

Task 2 Analyze all drugs

Task 2.1 Drug category statistics

Task 2.2 Drug Sales Proportion

Task 2.3 Drug Monthly Sales Curve 

Task 3 Analyze all drug brands

Task 3.1 Statistics of Drug Brand Types

Task 3.2 Sales Proportion of Drug Brands 

Task 3.3 Drug Brand Sales Analysis

task 4 

task 5

Summarize 


Topic: With the gradual liberalization of national policies, more and more medicines can be purchased online, and the pharmaceutical e-commerce platform is booming. Affected by the new crown epidemic, it is difficult to purchase offline pharmacies, which makes pharmaceutical e-commerce enter more consumption From the perspective of investors, major pharmaceutical companies have also stepped up their efforts in the field of pharmaceutical e-commerce. However, the e-commerce model is different from offline retail. How to better manage pharmaceutical e-commerce has become an urgent problem for pharmaceutical companies to solve. This question collects Tmall vitamin medicines. Please clean, analyze and mine data on vitamin medicines, and answer the following questions.

Task 1 Analyze the store

Analyze the stores, how many stores are included in total, and what is the sales ratio of each store? Give the store with the highest proportion of sales, and analyze the sales of the store.

Task 1.1  Store Type Statistics

Import data through the read_excel function of pandas, use the value_counts function to count the "shop_name" column, and get a total of 26 shops

import pandas as pd
data=pd.read_excel('F:\\2022全国大学生数据分析A题\\data\\data.xlsx')
data["shop_name"].value_counts()
Ali Health Pharmacy 16958
Tmall International Import Supermarket 7760
Kang Aiduo Pharmacy Flagship Store 4051
Tmall Supermarket 3764
ChemistWarehouse overseas flagship store 3205
Jiaozuo Guokong Pharmacy Flagship Store 3027
Ali Health Pharmacy Overseas Store 2938
NYC Merrill Lynch Health Overseas Franchise Store 2823
thejamy health care overseas franchise store 2345
Baikang Health Products Store 2224
Conba official flagship store 2196
hihealth overseas franchise store 2196
Yidu Overseas Franchise Store 2165
Tmall International Miaoyan Society 1755
NRC Nutrition Optimal Overseas Franchise Store 1742
Suning.com official flagship store 1681
nrfs lakeside overseas franchise store 1559
Imported/domestic health products boutique 1525
Kangshou Nutrition Store 1516
Baixiu Pharmacy Flagship Store 1505
LuckyVitamin Overseas Flagship Store 1497
skyshop overseas franchise store 1435
Moving health hall 1423
American and Canadian Boutique 1373
SASA US direct mail 1228
Tongtang Health Products Store 1219
Name: shop_name, dtype: int64

Ali Health Pharmacy

Tmall International Import Supermarket

Kang Aiduo Pharmacy Flagship Store

Tmall Supermarket

SASA US direct mail

Jiaozuo Guokong Pharmacy Flagship Store

Ali Health Pharmacy Overseas Store

Tongtang Health Products Store

skyshop overseas franchise store

Baikang Health Products Store

hihealth overseas franchise store

Conba official flagship store

Yidu overseas franchise store

Tmall International Miaoyan Society

Moving Health Pavilion

Suning.com official flagship store

nrfs lakeside overseas franchise store

Imported/domestic health products boutique

Kangshou Nutrition Store

Baixiu Pharmacy Flagship Store

American and Canadian Boutique

NYC Merrill Lynch Health Overseas Franchise Store

LuckyVitamin Overseas Flagship Store

ChemistWarehouse Overseas Flagship Store

NRC Nutrition Optimal Overseas Franchise Store

thejamy health care overseas franchise store

Task 1.2  Proportion of store sales

First, fill all the empty values ​​in the "discount" column with "10% off" in Excel, that is, no discount, and then calculate the sales according to the formula. The formula is as follows: Use the group_by function to calculate the total sales of each store and find its proportion .

data["discount"].value_counts()
15% off 32338
10% off 29929
15% off 9959
10% off 2884
Name: discount, dtype: int64
discount_num=[]
for i in range(len(data["shop_name"])):
    discount_num.append(0)
for i in range(len(data["shop_name"])):
    if data.loc[i,"discount"]=="10折":
        discount_num[i]=1.0 
    if data.loc[i,"discount"]=="9.5折":
        discount_num[i]=0.95
    if data.loc[i,"discount"]=="9折":
        discount_num[i]=0.90
    if data.loc[i,"discount"]=="8.5折":
        discount_num[i]=0.85 
df_1=pd.DataFrame({'discount_num':discount_num})
df_1.to_excel('F:\\2022全国大学生数据分析A题\\data\\result_1.xlsx',sheet_name='sheet1',index=None)
total_price=[]
for i in range(len(data["shop_name"])):    
    total_price.append(0)
for i in range(len(data["shop_name"])):
    price=data.loc[i,"price"]
    sold=float(data.loc[i,"sold"])
    discount=discount_num[i]
    total_price[i]=float(price)*float(sold)*float(discount)
df_2=pd.DataFrame({'total_price':total_price})
df_2.to_excel('F:\\2022全国大学生数据分析A题\\data\\result_2.xlsx',sheet_name='sheet1',index=None)
data1=pd.read_excel('F:\\2022全国大学生数据分析A题\\data\\result_2.xlsx')
data1
-----------------------------------------------------------------------
        shop_name	total_price
0	宜度海外专营店	1041.390
1	天猫国际进口超市	31950.000
2	阿里健康大药房	7794.275
3	天猫国际进口超市	12029.850
4	阿里健康大药房	10704.000
...	...	...
75105	阿里健康大药房	182044.800
75106	阿里健康大药房海外店	34938.000
75107	hihealth海外专营店	7416.900
75108	阿里健康大药房	7871.850
75109	阿里健康大药房	19806.840
75110 rows × 2 columns

total_shop = data1.groupby("shop_name")["total_price"].sum().reset_index()
total_shop=total_shop.sort_values("total_price",ascending=True)
total_shop
----------------------------------------------------------------------------
        shop_name	total_price
9	同堂保健品专营店	6.539738e+04
3	SASA美国直邮	3.258235e+05
1	LuckyVitamin海外旗舰店	3.697916e+05
14	康寿营养品店	6.198878e+05
21	美加精品	6.466264e+05
23	进口/国产保健品精品店	7.276764e+05
17	搬运健康馆	7.948006e+05
5	nrfs湖畔海外专营店	8.727912e+05
2	NRC营养优选海外专营店	1.408813e+06
18	焦作国控大药房旗舰店	1.546531e+06
20	百秀大药房旗舰店	2.843618e+06
4	hihealth海外专营店	3.382795e+06
6	nyc美林健康海外专营店	4.343254e+06
16	康爱多大药房旗舰店	4.986436e+06
19	百康保健品专营店	5.288855e+06
13	宜度海外专营店	6.001669e+06
7	skyshop海外专营店	6.506159e+06
8	thejamy保健海外专营店	9.798825e+06
22	苏宁易购官方旗舰店	1.065959e+07
10	天猫国际妙颜社	3.569804e+07
0	ChemistWarehouse海外旗舰店	5.539680e+07
15	康恩贝官方旗舰店	9.559872e+07
25	阿里健康大药房海外店	1.065089e+08
12	天猫超市	1.148217e+08
11	天猫国际进口超市	3.561012e+08
24	阿里健康大药房	6.751027e+08
total_shop = data1.groupby("shop_name")["total_price"].sum().reset_index()
total_shop
---------------------------------------------------------------------------
        shop_name	total_price
0	ChemistWarehouse海外旗舰店	5.539680e+07
1	LuckyVitamin海外旗舰店	3.697916e+05
2	NRC营养优选海外专营店	1.408813e+06
3	SASA美国直邮	3.258235e+05
4	hihealth海外专营店	3.382795e+06
5	nrfs湖畔海外专营店	8.727912e+05
6	nyc美林健康海外专营店	4.343254e+06
7	skyshop海外专营店	6.506159e+06
8	thejamy保健海外专营店	9.798825e+06
9	同堂保健品专营店	6.539738e+04
10	天猫国际妙颜社	3.569804e+07
11	天猫国际进口超市	3.561012e+08
12	天猫超市	1.148217e+08
13	宜度海外专营店	6.001669e+06
14	康寿营养品店	6.198878e+05
15	康恩贝官方旗舰店	9.559872e+07
16	康爱多大药房旗舰店	4.986436e+06
17	搬运健康馆	7.948006e+05
18	焦作国控大药房旗舰店	1.546531e+06
19	百康保健品专营店	5.288855e+06
20	百秀大药房旗舰店	2.843618e+06
21	美加精品	6.466264e+05
22	苏宁易购官方旗舰店	1.065959e+07
23	进口/国产保健品精品店	7.276764e+05
24	阿里健康大药房	6.751027e+08
25	阿里健康大药房海外店	1.065089e+08
total_shop_new = total_shop
for i in range(len(total_shop["total_price"])):
    total_shop_new["total_price"][i]=(total_shop_new["total_price"][i])/total
total_shop_new
------------------------------------------------------------------------------
            shop_name	total_price
0	ChemistWarehouse海外旗舰店	0.036921
1	LuckyVitamin海外旗舰店	0.000246
2	NRC营养优选海外专营店	0.000939
3	SASA美国直邮	0.000217
4	hihealth海外专营店	0.002255
5	nrfs湖畔海外专营店	0.000582
6	nyc美林健康海外专营店	0.002895
7	skyshop海外专营店	0.004336
8	thejamy保健海外专营店	0.006531
9	同堂保健品专营店	0.000044
10	天猫国际妙颜社	0.023792
11	天猫国际进口超市	0.237335
12	天猫超市	0.076527
13	宜度海外专营店	0.004000
14	康寿营养品店	0.000413
15	康恩贝官方旗舰店	0.063715
16	康爱多大药房旗舰店	0.003323
17	搬运健康馆	0.000530
18	焦作国控大药房旗舰店	0.001031
19	百康保健品专营店	0.003525
20	百秀大药房旗舰店	0.001895
21	美加精品	0.000431
22	苏宁易购官方旗舰店	0.007104
23	进口/国产保健品精品店	0.000485
24	阿里健康大药房	0.449943
25	阿里健康大药房海外店	0.070986
total_shop=total_shop.sort_values("total_price",ascending=True)
total_shop
---------------------------------------------------------------------
       shop_name	total_price
9	同堂保健品专营店	0.000044
3	SASA美国直邮	0.000217
1	LuckyVitamin海外旗舰店	0.000246
14	康寿营养品店	0.000413
21	美加精品	0.000431
23	进口/国产保健品精品店	0.000485
17	搬运健康馆	0.000530
5	nrfs湖畔海外专营店	0.000582
2	NRC营养优选海外专营店	0.000939
18	焦作国控大药房旗舰店	0.001031
20	百秀大药房旗舰店	0.001895
4	hihealth海外专营店	0.002255
6	nyc美林健康海外专营店	0.002895
16	康爱多大药房旗舰店	0.003323
19	百康保健品专营店	0.003525
13	宜度海外专营店	0.004000
7	skyshop海外专营店	0.004336
8	thejamy保健海外专营店	0.006531
22	苏宁易购官方旗舰店	0.007104
10	天猫国际妙颜社	0.023792
0	ChemistWarehouse海外旗舰店	0.036921
15	康恩贝官方旗舰店	0.063715
25	阿里健康大药房海外店	0.070986
12	天猫超市	0.076527
11	天猫国际进口超市	0.237335
24	阿里健康大药房	0.449943
total_shop=total_shop.sort_values("total_price",ascending=True)
total_shop
-------------------------------------------------------------------
        shop_name	total_price
9	同堂保健品专营店	0.000044
3	SASA美国直邮	0.000217
1	LuckyVitamin海外旗舰店	0.000246
14	康寿营养品店	0.000413
21	美加精品	0.000431
23	进口/国产保健品精品店	0.000485
17	搬运健康馆	0.000530
5	nrfs湖畔海外专营店	0.000582
2	NRC营养优选海外专营店	0.000939
18	焦作国控大药房旗舰店	0.001031
20	百秀大药房旗舰店	0.001895
4	hihealth海外专营店	0.002255
6	nyc美林健康海外专营店	0.002895
16	康爱多大药房旗舰店	0.003323
19	百康保健品专营店	0.003525
13	宜度海外专营店	0.004000
7	skyshop海外专营店	0.004336
8	thejamy保健海外专营店	0.006531
22	苏宁易购官方旗舰店	0.007104
10	天猫国际妙颜社	0.023792
0	ChemistWarehouse海外旗舰店	0.036921
15	康恩贝官方旗舰店	0.063715
25	阿里健康大药房海外店	0.070986
12	天猫超市	0.076527
11	天猫国际进口超市	0.237335
24	阿里健康大药房	0.449943
import matplotlib.pyplot as plt
import seaborn as sns
import plotly.express as px
plt.rcParams['font.family'] = ['sans-serif']
plt.rcParams['font.sans-serif'] = ['SimHei']
#调节图形大小,宽,高
plt.figure(figsize=(9,6))
#定义饼状图的标签,标签是列表
labels = [u"阿里健康大药房",u"天猫国际进口超市",u"其它"]
#每个标签占多大,会自动去算百分比
# sizes = [0.458,0.236,0.071,0.069,0.064,0.037,0.022,0.043]
# colors = ['red','yellowgreen','lightskyblue','cyan','lightseagreen','lime','darkorange','pink']
sizes = [0.450,0.237,0.313]
colors = ['red','yellowgreen','lightskyblue']
#将某部分爆炸出来, 使用括号,将第一块分割出来,数值的大小是分割出来的与其他两块的间隙
explode = (0.05,0,0)
 
patches,l_text,p_text = plt.pie(sizes,explode=explode,labels=labels,colors=colors,
                                labeldistance = 1.1,autopct = '%3.1f%%',shadow = False,
                                startangle = 90,pctdistance = 0.6)
 
#labeldistance,文本的位置离远点有多远,1.1指1.1倍半径的位置
#autopct,圆里面的文本格式,%3.1f%%表示小数有三位,整数有一位的浮点数
#shadow,饼是否有阴影
#startangle,起始角度,0,表示从0开始逆时针转,为第一块。一般选择从90度开始比较好看
#pctdistance,百分比的text离圆心的距离
#patches, l_texts, p_texts,为了得到饼图的返回值,p_texts饼图内部文本的,l_texts饼图外label的文本
 
#改变文本的大小
#方法是把每一个text遍历。调用set_size方法设置它的属性
for t in l_text:
    t.set_size(15)
for t in p_text:
    t.set_size(20)
# 设置x,y轴刻度一致,这样饼图才能是圆的
plt.axis('equal')
plt.title('药店销售额占比图', size=20)
plt.legend()
plt.show()

plt.figure(figsize=(15,6),dpi=200)
x=total_shop["shop_name"]
plt.bar(x, total_shop["total_price"], color='#87CEFA')
# plt.xlabel('时间')
plt.xticks(total_shop["shop_name"],rotation=60)
plt.ylabel('销售额占比')
plt.title('各药店销售额占比')
plt.show()

Task 1.3  Sales of Ali Health Pharmacy

data2=data[data["shop_name"]=="阿里健康大药房"]
data2["discount"].value_counts()
10.5% off 10768
10% off 4953
10% off 663
15% off 574
Name: discount, dtype: int64
plt.figure(figsize=(9,6))
labels = [u"9.5折",u"9折",u"8.5折",u"无打折"]
sizes = [10768,4953,574,663]
colors = ['orange','yellowgreen','lightskyblue','pink']
explode = (0.05,0,0,0)
patches,l_text,p_text = plt.pie(sizes,explode=explode,labels=labels,colors=colors,
                                labeldistance = 1.1,autopct = '%3.1f%%',shadow = False,
                                startangle = 90,pctdistance = 0.6) 
for t in l_text:
    t.set_size(12)
for t in p_text:
    t.set_size(12)
plt.axis('equal')
plt.title('阿里健康大药房打折情况', size=15)
plt.legend()
plt.show()

df3=data2.sort_values(by="sold" , ascending=False)
df3
--------------------------------------------------------------
        date_time	    id            	shop_name	        title	                                sku_name	price	        sold	    discount	    brand	    parameter
58239	2021-12-01	4418809521757	阿里健康大药房	3盒】星鲨维生素D滴剂(胶囊型)24粒补钙维生素D3佝偻病官方婴儿钙	3盒装 | 拍2优惠 | 维D促进钙吸收	106.666667	103940	9折	NaN	药品名称:维生素D滴剂(胶囊型)||生产企业:国药控股星鲨制药(厦门)有限公司||产品名称:...
26620	2021-06-01	4543217834215	阿里健康大药房	3盒】星鲨维生素D滴剂(胶囊型)24粒补钙维生素D佝偻病官方婴儿钙	3盒装 | 拍2优惠| 促进钙吸收	109.000000	73061	9.5折	NaN	药品名称:维生素D滴剂(胶囊型)||生产企业:国药控股星鲨制药(厦门)有限公司||产品名称:...
31883	2021-08-01	4418809521757	阿里健康大药房	3盒】星鲨维生素D滴剂(胶囊型)24粒补钙维生素D佝偻病官方婴儿钙	3盒装 | 拍2更优惠 | D促进钙吸收	109.000000	70625	9.5折	NaN	药品名称:维生素D滴剂(胶囊型)||生产企业:国药控股星鲨制药(厦门)有限公司||产品名称:...
74446	2021-02-01	3435079967513	阿里健康大药房	恒健维生素C片VC 维C100片补充维生素C妊娠呕吐发少发黄vc片药用	标准装1盒	3.400000	45895	9折	NaN	药品名称:维生素C片||生产企业:广东恒健制药有限公司||规格:100mg*100片/片||...
47489	2021-02-01	571172601110	阿里健康大药房	CONBA/康恩贝 维生素C咀嚼片(香橙味) 1.2g/片*100片补充维C VC	NaN	18.900000	44511	8.5折	NaN	厂名:华润圣海健康科技有限公司||产品名称:CONBA/康恩贝 维生素C咀嚼片(香橙味) 1...
...	...	...	...	...	...	...	...	...	...	...
70770	2021-09-01	559409418908	阿里健康大药房	礼盒装汤臣倍健维生素C片vc片维C咀嚼片E官网天然维生素e软胶囊ve	NaN	109.000000	0	9折	NaN	品牌:汤臣倍健||品牌:BY-HEALTH/汤臣倍健||系列:维生素C片(橘子味)+维生素E...
44963	2021-01-01	3621111355030	阿里健康大药房	恒健维生素C片VC 维C100mg*100片/瓶补充维生素C妊娠呕吐发少发黄	1盒本品+川奇3+1牌铁锌钙氨基酸口服液 10ml/支*10支/盒	13.000000	0	8.5折	恒健	药品名称:维生素C片||生产企业:广东恒健制药有限公司||规格:100mg*100片/片||...
11440	2021-05-01	4475310558004	阿里健康大药房	海迪威维生素C泡腾片15片过敏维生素缺乏坏血病vc片泡腾 维c泡腾	套餐二:5盒,家庭装	93.000000	0	9.5折	NaN	产品名称:海迪威 维生素C泡腾片 0.5g*15片/盒||品牌:海迪威||药品通用名:维生素...
14259	2021-03-01	4475654714725	阿里健康大药房	亚宝维和维生素C泡腾片15片增强免疫力 过敏 预防传染性疾病 流感	套餐一:3盒常备装	115.000000	0	9.5折	NaN	药品通用名:维生素C泡腾片||规格:1g*15片/盒||药品名称:维生素C泡腾片||生产企业...
69475	2021-04-01	3619151867229	阿里健康大药房	呀吖维生素AD滴剂(胶囊型)20粒儿童补充维生素AD抽搐夜盲症佝偻病	1盒本品+贝亲婴儿润肤乳70G	53.300000	0	9折	NaN	品牌:呀吖||产品名称:呀吖 维生素AD滴剂(胶囊型) 20粒/盒||药品通用名:维生素AD...
16958 rows × 10 columns

 

data3=pd.read_excel('F:\\2022全国大学生数据分析A题\\data\\new_data.xlsx')
data4=data3[data3["shop_name"]=="阿里健康大药房"]
total_month= data4.groupby("date_time")["total_price"].sum().reset_index()
total_month
----------------------------------------------------------------------------
     date_time	  total_price
0	2020-01-01	1.092576e+07
1	2020-02-01	9.885606e+06
2	2020-03-01	1.656051e+07
3	2020-04-01	1.579910e+07
4	2020-05-01	1.383416e+07
5	2020-06-01	2.813641e+07
6	2020-07-01	1.408467e+07
7	2020-08-01	1.767333e+07
8	2020-09-01	1.865158e+07
9	2020-10-01	1.443941e+07
10	2020-11-01	1.935696e+07
11	2020-12-01	3.204151e+07
12	2021-01-01	2.240306e+07
13	2021-02-01	2.787846e+07
14	2021-03-01	3.541297e+07
15	2021-04-01	2.932532e+07
16	2021-05-01	3.180636e+07
17	2021-06-01	6.001385e+07
18	2021-07-01	3.384237e+07
19	2021-08-01	3.590081e+07
20	2021-09-01	3.550590e+07
21	2021-10-01	4.110051e+07
22	2021-11-01	4.706524e+07
23	2021-12-01	6.345886e+07
plt.figure(figsize=(15,6),dpi=200)
x=["2020-01","2020-02","2020-03","2020-04","2020-05","2020-06","2020-07","2020-08","2020-09","2020-10","2020-11","2020-12",
   "2021-01","2021-02","2021-03","2021-04","2021-05","2021-06","2021-07","2021-08","2021-09","2021-10","2021-11","2021-12"]
y=total_month["total_price"]
plt.plot(x, y, color='orange')
plt.xlabel('时间')
plt.xticks(x,rotation=70)
plt.ylabel('当月销售额')
plt.title('阿里健康大药房2020-2021年各月份销售额')
plt.show()

data5=data4.groupby("title")["sold"].sum().reset_index()
data5.sort_values(by="sold" , ascending=False)
---------------------------------------------------------------------------
                title	                                      sold
363	丽点维A酸乳膏20g去闭口粉刺暗疮软膏维生素a维a乳酸膏乳软膏祛痘	404107
105	3盒】星鲨维生素D滴剂(胶囊型)24粒补钙维生素D佝偻病官方婴儿钙	267120
1190	浙江医药维生素E软胶囊30粒心脑血管疾病 习惯性流产不孕ve胶囊	252963
9	21金维他多维元素片100片复合维生素B b6 b1 b2维生素c钙铁锌21	232891
1026	星鲨维生素D滴剂(胶囊型)30粒儿童补钙补充维生素D发育迟缓佝偻病	227536
...	...	...
1614	限量买1送2 汤臣倍健R葡萄籽维生素C加E片 410mg/片*90片VCVE成人	0
1372	维生素B2片5mg*100片/瓶脂溢性皮炎口角炎唇干裂结膜炎阴囊炎	0
1108	汤臣倍健 维生素C片(橘子味)+维生素E60粒礼盒天然维生素e维C	0
1133	汤臣倍健R葡萄籽维生素C加E片 410mg/片*90片VCVE成人	0
1268	礼盒装汤臣倍健维生素C片vc片维C咀嚼片E官网天然维生素e软胶囊ve	0
1663 rows × 2 columns
data5=data4.groupby("title")["total_price"].sum().reset_index()
data5.sort_values(by="total_price" , ascending=False)
-------------------------------------------------------------------
                                    title                	total_price
1637	领券减】爱乐维复合维生素100片孕妇叶酸补充备孕早期多维元素B	3.569218e+07
105	3盒】星鲨维生素D滴剂(胶囊型)24粒补钙维生素D佝偻病官方婴儿钙	2.836576e+07
116	3盒包邮】达因伊可新维生素AD滴剂30粒1岁以上AD预防佝偻病ad滴剂	1.622559e+07
104	3盒】星鲨维生素D滴剂(胶囊型)24粒补钙维生素D3佝偻病官方婴儿钙	1.612723e+07
9	21金维他多维元素片100片复合维生素B b6 b1 b2维生素c钙铁锌21	1.508954e+07
...	...	...
1133	汤臣倍健R葡萄籽维生素C加E片 410mg/片*90片VCVE成人	0.000000e+00
171	BY-HEALTH/汤臣倍健 维生素C片(橘子味)+维生素E60粒礼盒	0.000000e+00
1614	限量买1送2 汤臣倍健R葡萄籽维生素C加E片 410mg/片*90片VCVE成人	0.000000e+00
1372	维生素B2片5mg*100片/瓶脂溢性皮炎口角炎唇干裂结膜炎阴囊炎	0.000000e+00
1108	汤臣倍健 维生素C片(橘子味)+维生素E60粒礼盒天然维生素e维C	0.000000e+00
1663 rows × 2 columns

Task 2 Analyze all drugs

Analyze all drugs, how many drugs are included in total, and what is the sales ratio of each drug? Give the 10 drugs with the highest sales ratio, and draw the monthly sales curve of these 10 drugs.

Task 2.1 Drug category statistics

import pandas as pd
data=pd.read_excel('F:\\2022全国大学生数据分析A题\\data\\new_data.xlsx')
data["id"].value_counts()
------------------------------------------------------------------------
3566974752915    24
564979974262     24
3601587058427    24
3232160081409    24
544066576806     24
                 ..
4391874217388     1
4629962755717     1
544106976534      1
4631336339308     1
590797340368      1
Name: id, Length: 9697, dtype: int64

Task 2.2 Drug Sales Proportion

total_id=data.groupby("id")["total_price"].sum().reset_index()
total_id=total_id.sort_values("total_price",ascending=False)
total_id
------------------------------------------------------------------
            id	        total_price
6594	4169804230645	3.975427e+07
8951	4521420762240	2.521464e+07
5311	3415285443577	2.503826e+07
8093	4418809521757	2.403036e+07
1058	542510921766	1.787574e+07
...	...	...
6270	4012427304925	0.000000e+00
6271	4012427304926	0.000000e+00
6272	4012427304927	0.000000e+00
6273	4012427304928	0.000000e+00
6499	4135649044700	0.000000e+00
9697 rows × 2 columns
id_ten=total_id.head(10)
id_ten
----------------------------------------------------
                id	    total_price
6594	4169804230645	3.975427e+07
8951	4521420762240	2.521464e+07
5311	3415285443577	2.503826e+07
8093	4418809521757	2.403036e+07
1058	542510921766	1.787574e+07
1747	551699740249	1.625078e+07
5306	3414540274727	1.316437e+07
9055	4543217834215	1.287603e+07
5400	3452991932319	1.238747e+07
7207	4296851710480	1.211168e+07
total=total_id["total_price"].sum()
total
-------------------------------------------------
1500417429.02092
id_sold_percent=[]
for i in range(len(id_ten["id"])):
    id_sold_percent.append(0)
for i in range(len(id_ten["id"])):
    id_sold_percent[i]=list(id_ten["total_price"])[i]/total
id_sold_percent
--------------------------------------------------------------
[0.02649547009406077,
 0.01680508355154942,
 0.016687532759692366,
 0.01601578263171197,
 0.011913841790457338,
 0.010830836469024661,
 0.00877380237351032,
 0.008581630718860754,
 0.008256018576608164,
 0.008072208383971744]

Task 2.3 Drug Monthly Sales Curve 

data_yp1=data[data["id"]==4169804230645]
#data_yp1
total_month_yp1= data_yp1.groupby("date_time")["total_price"].sum().reset_index()
total_month_yp1
data_yp2=data[data["id"]==4521420762240]
# data_yp2
total_month_yp2= data_yp2.groupby("date_time")["total_price"].sum().reset_index()
total_month_yp2
data_yp3=data[data["id"]==3415285443577]
# data_yp3
total_month_yp3= data_yp3.groupby("date_time")["total_price"].sum().reset_index()
total_month_yp3
data_yp4=data[data["id"]==4418809521757]
# data_yp4
total_month_yp4= data_yp4.groupby("date_time")["total_price"].sum().reset_index()
total_month_yp4
data_yp5=data[data["id"]==542510921766]
# data_yp5
total_month_yp5= data_yp5.groupby("date_time")["total_price"].sum().reset_index()
total_month_yp5
data_yp6=data[data["id"]==551699740249]
# data_yp6
total_month_yp6= data_yp6.groupby("date_time")["total_price"].sum().reset_index()
total_month_yp6
data_yp7=data[data["id"]==3414540274727]
# data_yp7
total_month_yp7= data_yp7.groupby("date_time")["total_price"].sum().reset_index()
total_month_yp7
data_yp8=data[data["id"]==4543217834215]
# data_yp8
total_month_yp8= data_yp8.groupby("date_time")["total_price"].sum().reset_index()
total_month_yp8
data_yp9=data[data["id"]==3452991932319]
data_yp9
total_month_yp9= data_yp9.groupby("date_time")["total_price"].sum().reset_index()
total_month_yp9
data_yp10=data[data["id"]==4296851710480]
data_yp10
total_month_yp10= data_yp10.groupby("date_time")["total_price"].sum().reset_index()
total_month_yp10
import matplotlib.pyplot as plt
import seaborn as sns
import plotly.express as px
plt.rcParams['font.family'] = ['sans-serif']
plt.rcParams['font.sans-serif'] = ['SimHei']

plt.figure(figsize=(20,30),dpi=200)
ax=plt.subplot(5,2,1)
x_1=["2020-7","2020-8","2020-9","2020-10","2020-11","2020-12","2021-01","2021-02","2021-03","2021-04","2021-05","2021-06",
     "2021-07","2021-08","2021-09","2021-10","2021-11","2021-12"]
y_1=total_month_yp1["total_price"]
plt.plot(x_1, y_1, color='orange')
plt.xticks(x_1,rotation=70)
plt.ylabel('当月销售额')
plt.title('id:4169804230645月份销售额')

ax=plt.subplot(5,2,2)
x_2=["2021-04","2021-05","2021-06","2021-07","2021-08","2021-10","2021-12",]
y_2=total_month_yp2["total_price"]
plt.plot(x_2, y_2, color='orange')
plt.xticks(x_2,rotation=70)
plt.ylabel('当月销售额')
plt.title('id:4521420762240月份销售额')

ax=plt.subplot(5,2,3)
x_3=["2020-01","2020-02","2020-03","2020-04","2020-05","2020-06","2020-08","2020-09","2020-10","2020-12","2021-01","2021-02","2021-03"]
y_3=total_month_yp3["total_price"]
plt.plot(x_3, y_3, color='orange')
plt.xticks(x_3,rotation=70)
plt.ylabel('当月销售额')
plt.title('id:3415285443577月份销售额')

ax=plt.subplot(5,2,4)
x_4=["2021-07","2021-08","2021-10","2021-11","2021-12"]
y_4=total_month_yp4["total_price"]
plt.plot(x_4, y_4, color='orange')
plt.xticks(x_4,rotation=70)
plt.ylabel('当月销售额')
plt.title('id:4418809521757月份销售额')

ax=plt.subplot(5,2,5)
x_5=["2020-03","2020-04","2020-05","2020-06","2020-07","2020-08","2020-09","2020-10","2020-11","2020-12",
     "2021-01","2021-02","2021-03","2021-04","2021-05","2021-06","2021-07","2021-08","2021-09","2021-10","2021-11","2021-12"]
y_5=total_month_yp5["total_price"]
plt.plot(x_5, y_5, color='orange')
plt.xticks(x_5,rotation=70)
plt.ylabel('当月销售额')
plt.title('id:542510921766月份销售额')

ax=plt.subplot(5,2,6)
x_6=["2020-01","2020-02","2020-03","2020-04","2020-05","2020-06","2020-07","2020-08","2020-09","2020-10","2020-11","2020-12",
     "2021-01","2021-02"]
y_6=total_month_yp6["total_price"]
plt.plot(x_6, y_6, color='orange')
plt.xticks(x_6,rotation=70)
plt.ylabel('当月销售额')
plt.title('id:551699740249月份销售额')

ax=plt.subplot(5,2,7)
x_7=["2020-01","2020-02",
   "2020-03","2020-04","2021-03","2021-04","2021-07"]
y_7=total_month_yp7["total_price"]
plt.plot(x_7, y_7, color='orange')
plt.xticks(x_7,rotation=70)
plt.ylabel('当月销售额')
plt.title('id:3414540274727月份销售额')

ax=plt.subplot(5,2,8)
x_8=["2021-05","2021-06","2021-07"]
y_8=total_month_yp8["total_price"]
plt.plot(x_8, y_8, color='orange')
plt.xticks(x_8,rotation=70)
plt.ylabel('当月销售额')
plt.title('id:4543217834215月份销售额')

ax=plt.subplot(5,2,9)
x_9=["2020-01","2020-02","2020-03","2020-04","2020-05","2020-06","2020-07","2020-08","2020-09","2020-10","2020-11","2020-12",
    "2021-01","2021-02","2021-03","2021-04","2021-05","2021-06","2021-07","2021-08","2021-10","2021-11","2021-12"]
y_9=total_month_yp9["total_price"]
plt.plot(x_9, y_9, color='orange')
plt.xticks(x_9,rotation=70)
plt.ylabel('当月销售额')
plt.title('id:3452991932319月份销售额')

ax=plt.subplot(5,2,10)
x_10=["2020-01","2020-05","2020-06","2020-07","2020-09","2020-10","2020-11","2020-12",
   "2021-01","2021-02"]
y_10=total_month_yp10["total_price"]
plt.plot(x_10, y_10, color='orange')
plt.xticks(x_10,rotation=70)
plt.ylabel('当月销售额')
plt.title('id:4296851710480月份销售额')
plt.show()

Task 3 Analyze all drug brands

Analyze all drug brands, how many brands are included in total, and what is the sales ratio of each brand? Give the 10 brands with the highest sales ratio, and analyze the reasons why these 10 brands sell better?

Task 3.1 Statistics of Drug Brand Types

import pandas as pd
data=pd.read_excel('F:\\2022全国大学生数据分析A题\\data\\new_data.xlsx')
import numpy as np
data.dropna(axis=0,subset = ['parameter'],inplace=True)
data = data.reset_index(drop=True)
brand=[]
for i in range(len(data["parameter"])):
    brand.append(0)
for i in range(len(data["parameter"])):
    str1=data.loc[i,"parameter"].replace("||",",")
    #print(i)
    parameter_dict = {}
    content=str1.splitlines()
    str2=content[0].split(',')
    for j in str2:
        k, v = j.split(':')
        parameter_dict[k] = v
    brand_parameter=parameter_dict["品牌"]
    brand[i]=brand_parameter
data["brand"]=brand
data
-----------------------------------------------------------------
        date_time	id	shop_name	    title	        sku_name	price	sold	discount	total_price	brand	parameter
0	2020-11-01	3985971101463	宜度海外专营店	特价处理!超低价19.9元起!含维生素ABCDE B族B12 维生素锌硒片	维生素e 300粒--买3送1	60.900000	19	9折	1041.390	Puritan‘s Pride/普丽普莱	厂名:Puritan's Pride||品牌:Puritan‘s Pride/普丽普莱||生...
1	2020-11-01	4224064440294	天猫国际进口超市	FANCL男40岁综合营养包30包/袋*2复合多种维生素日本芳珂fancl	(【直营】日本FANCL/芳珂 40岁男性综合维生素营养 30袋(30天量))*2	500.000000	71	9折	31950.000	FANCL	品牌:FANCL||系列:综合营养包||生产企业:FANCL株式会社
2	2021-12-01	3437759489038	阿里健康大药房	民生21金维他多维元素片30片儿童男女成人补充维生素矿物质缺乏	国民大牌| 补充21种维矿	22.416667	366	9.5折	7794.275	21金维他	产品名称:21金维他 多维元素片(21) 30片*1瓶/盒||药品通用名:多维元素片(21)...
3	2021-12-01	576002931540	天猫国际进口超市	【直营】美国进口安利简装倍立健多种复合维生素矿物质营养186片	NaN	452.250000	28	9.5折	12029.850	AMWAY/安利	厂名:Amway Dominican Republic LLC||生产企业:Amway||系...
4	2021-03-01	4363644898037	阿里健康大药房	买2送礼】elevit/爱乐维复合维生素片140粒维生素缺乏症贫血官方	爱乐维玛咖锌淫羊藿胶囊140粒+爱乐维 复合维生素片30片/盒	371.666667	32	9折	10704.000	elevit/爱乐维	品牌:elevit/爱乐维||药品名称:复合维生素片||药品通用名:复合维生素片||生产企业...
...	...	...	...	...	...	...	...	...	...	...	...
72553	2021-03-01	3435079967513	阿里健康大药房	恒健维生素C片VC 维C100片补充维生素C妊娠呕吐发少发黄vc片药用	标准装1盒	6.000000	33712	9折	182044.800	恒健	药品名称:维生素C片||生产企业:广东恒健制药有限公司||规格:100mg*100片/片||...
72554	2021-10-01	568442923286	阿里健康大药房海外店	日本参天维生素B12+E眼药水*12ml 滴眼液 缓解眼疲劳 眼疾预防	NaN	60.000000	647	9折	34938.000	参天	品牌:Santen/参天||药品通用名:滴眼液||生产企业:参天製薬株式会社||药品规格:1...
72555	2020-03-01	534742130942	hihealth海外专营店	【3倍购买】美国GNC综合营养包30天套装 含钙片/DHA/维生素	NaN	201.000000	41	9折	7416.900	GNC/健安喜	系列:维生素矿物质精华(孕妇)||厂名:General Nutrition Corporat...
72556	2020-04-01	3733029515023	阿里健康大药房	星鲨维生素E软胶囊100mg*60粒/盒心脑血管疾病 流产 不孕症	本品5盒 优惠装 节省2.5元	73.500000	119	9折	7871.850	星鲨	规格:100mg*60粒/盒||生产企业:国药控股星鲨制药(厦门)有限公司||产品名称:星鲨...
72557	2020-08-01	4068429729290	阿里健康大药房	力生 复合维生素B片100片/盒 维生素缺乏症脚气矿物质缺乏症厌食	标准装	7.400000	2974	9折	19806.840	力生	产品名称:力生 复合维生素B片 100片/盒||生产企业:天津力生制药股份有限公司||药品通...
72558 rows × 11 columns
data["brand"].value_counts().head(10)
---------------------------------------------------
BY-HEALTH/汤臣倍健          5675
CONBA/康恩贝               4196
GNC/健安喜                 3967
CENTRUM/善存              3394
swisse                  3205
汤臣倍健                    3186
养生堂                     2939
Puritan‘s Pride/普丽普莱    1907
Jamieson/健美生            1841
NOW/诺奥                  1546
Name: brand, dtype: int64
total_brand= data.groupby("brand")["total_price"].sum().reset_index()
total_brand.sort_values("total_price",inplace=True, ascending=False)
total_brand
-----------------------------------------------------------------------
    brand	total_price
317	swisse	2.821440e+08
55	CONBA/康恩贝	1.226368e+08
51	CENTRUM/善存	1.128456e+08
284	elevit/爱乐维	1.008569e+08
423	星鲨	9.825920e+07
...	...	...
45	Botanic Choice	8.550000e+01
370	吉医堂	8.000000e+01
186	OUT OF AFRICA	5.015000e+01
435	永昇金维	4.940000e+01
521	难老泉	4.200000e+01
530 rows × 2 columns

Task 3.2 Sales Proportion of Drug Brands 

total=total_brand["total_price"].sum()
ten_total_brand=total_brand.head(10)
ten_total_brand
------------------------------------------------
    brand	total_price
317	swisse	2.821440e+08
55	CONBA/康恩贝	1.226368e+08
51	CENTRUM/善存	1.128456e+08
284	elevit/爱乐维	1.008569e+08
423	星鲨	9.825920e+07
339	伊可新	5.991355e+07
355	养生堂	5.900897e+07
33	BY-HEALTH/汤臣倍健	4.588620e+07
85	FANCL	3.761334e+07
425	朗迪	3.281707e+07
brand_sold_percent=[]
for i in range(len(ten_total_brand["brand"])):
    brand_sold_percent.append(0)
for i in range(len(ten_total_brand["brand"])):
    brand_sold_percent[i]=list(total_brand["total_price"])[i]/total
brand_sold_percent
-----------------------------------------------------------------------
[0.18973373776485433,
 0.08246969958533347,
 0.07588545618742933,
 0.0678233886888395,
 0.06607649132273377,
 0.04029014581431487,
 0.03968184231814414,
 0.030857155568868446,
 0.02529389012975293,
 0.022068541829650644]
plt.figure(figsize=(8,6),dpi=100)
x_10=["swisse","CONBA/康恩贝","CENTRUM/善存","elevit/爱乐维","星鲨","伊可新","养生堂","BY-HEALTH/汤臣倍健","FANCL","朗迪"]
y_10=[18.97,8.25,7.59,6.78,6.61,4.03,3.97,3.09,2.53,2.21]
plt.barh(x_10, y_10, color='cyan')
plt.xlabel('品牌销售额占比 单位:%')
plt.ylabel('品牌')
plt.title('销售额占比最高的十大品牌销售额占比')
plt.show()

  

Task 3.3 Drug Brand Sales Analysis

data_pp1=data[data["brand"]=="swisse"]
data_pp1["discount"].value_counts()

----------------------------------------------

data_pp1["price"].mean()  #238.76599858001455

----------------------------------------------

top1_sold=data_pp1.sort_values("sold" ,ascending=False)
top1_sold.head(10)

----------------------------------------------

top1_total_price=data_pp1.sort_values("total_price" ,ascending=False)
top1_total_price.head(10)
data_pp2=data[data["brand"]=="CONBA/康恩贝"]
data_pp2["discount"].value_counts()

------------------------------------------------

data_pp2["price"].mean() #57.49846914842359

-----------------------------------------------------

top2_sold=data_pp2.sort_values("sold" ,ascending=False)
top2_sold.head(10)

-----------------------------------------------------
top2_total_price=data_pp2.sort_values("total_price" ,ascending=False)
top2_total_price.head(10)
data_pp3=data[data["brand"]=="CENTRUM/善存"]
data_pp3["discount"].value_counts()

--------------------------------------------------
data_pp3["price"].mean()

--------------------------------------------------

top3_sold=data_pp3.sort_values("sold" ,ascending=False)
top3_sold.head(10)

--------------------------------------------------
top3_total_price=data_pp3.sort_values("total_price" ,ascending=False)
top3_total_price.head(10)
data_pp4=data[data["brand"]=="elevit/爱乐维"]
data_pp4["discount"].value_counts()

----------------------------------------------------
data_pp4["price"].mean()
----------------------------------------------------

top4_sold=data_pp4.sort_values("sold" ,ascending=False)
top4_sold.head(10)

----------------------------------------------------
top4_total_price=data_pp4.sort_values("total_price" ,ascending=False)
top4_total_price.head(10)
data_pp5=data[data["brand"]=="星鲨"]
data_pp5["discount"].value_counts()

-------------------------------------------------
data_pp5["price"].mean()
-------------------------------------------------
top5_sold=data_pp5.sort_values("sold" ,ascending=False)
top5_sold.head(10)
-------------------------------------------------
top5_total_price=data_pp5.sort_values("total_price" ,ascending=False)
top5_total_price.head(10)
data_pp6=data[data["brand"]=="伊可新"]
data_pp6["discount"].value_counts()
---------------------------------------
data_pp6["price"].mean()
---------------------------------------
top6_sold=data_pp6.sort_values("sold" ,ascending=False)
top6_sold.head(10)
---------------------------------------
top6_total_price=data_pp6.sort_values("total_price" ,ascending=False)
top6_total_price.head(10)
data_pp7=data[data["brand"]=="养生堂"]
data_pp7["discount"].value_counts()
------------------------------------------
data_pp7["price"].mean()
----------------------------------------
top7_sold=data_pp7.sort_values("sold" ,ascending=False)
top7_sold.head(10)
----------------------------------------
top7_total_price=data_pp7.sort_values("total_price" ,ascending=False)
top7_total_price.head(10)
data_pp8=data[data["brand"]=="BY-HEALTH/汤臣倍健"]
data_pp8["discount"].value_counts()
------------------------------------------------

data_pp8["price"].mean()
------------------------------------------------
top8_sold=data_pp8.sort_values("sold" ,ascending=False)
top8_sold.head(10)
------------------------------------------------

top8_total_price=data_pp8.sort_values("total_price" ,ascending=False)
top8_total_price.head(10)
data_pp9=data[data["brand"]=="FANCL"]
data_pp9["discount"].value_counts()
---------------------------------------------
data_pp9["price"].mean()
---------------------------------------------
top9_sold=data_pp9.sort_values("sold" ,ascending=False)
top9_sold.head(10)
---------------------------------------------
top9_total_price=data_pp9.sort_values("total_price" ,ascending=False)
top9_total_price.head(10)
data_pp10=data[data["brand"]=="朗迪"]
data_pp10["discount"].value_counts()
---------------------------------------------
data_pp10["price"].mean()
---------------------------------------------
top10_sold=data_pp10.sort_values("sold" ,ascending=False)
top10_sold.head(10)
---------------------------------------------
top10_total_price=data_pp10.sort_values("total_price" ,ascending=False)
top10_total_price.head(10)
plt.figure(figsize=(20,18))
ax=plt.subplot(3,4,1)
labels_1 = [u"9折",u"9.5折",u"8.5折",u"无打折"]
#每个标签占多大,会自动去算百分比
sizes_1 = [2037,805,336,27]
colors = ['orange','yellowgreen','lightskyblue','yellow']
#将某部分爆炸出来, 使用括号,将第一块分割出来,数值的大小是分割出来的与其他两块的间隙
explode = (0.05,0,0,0)
 
patches,l_text,p_text = plt.pie(sizes_1,explode=explode,labels=labels_1,colors=colors,
                                labeldistance = 1.1,autopct = '%3.1f%%',shadow = False,
                                startangle = 90,pctdistance = 0.6)
for t in l_text:
    t.set_size(15)
for t in p_text:
    t.set_size(15)
# 设置x,y轴刻度一致,这样饼图才能是圆的
plt.axis('equal')
plt.title('swisse', size=15)
plt.legend(loc=2)

ax=plt.subplot(3,4,2)
labels_2 = [u"9.5折",u"9折",u"8.5折",u"无打折"]
sizes_2 = [2366,1384,311,135]
colors = ['orange','yellowgreen','lightskyblue','yellow']
explode = (0.05,0,0,0)
patches,l_text,p_text = plt.pie(sizes_2,explode=explode,labels=labels_2,colors=colors,
                                labeldistance = 1.1,autopct = '%3.1f%%',shadow = False,
                                startangle = 90,pctdistance = 0.6)
for t in l_text:
    t.set_size(15)
for t in p_text:
    t.set_size(15)
plt.axis('equal')
plt.title('CONBA/康恩贝', size=15)
plt.legend(loc=2)

ax=plt.subplot(3,4,3)
labels_3 = [u"9.5折",u"9折",u"8.5折",u"无打折"]
sizes_3 = [1699,1403,137,155]
colors = ['orange','yellowgreen','lightskyblue','yellow']
explode = (0.05,0,0,0)
patches,l_text,p_text = plt.pie(sizes_3,explode=explode,labels=labels_3,colors=colors,
                                labeldistance = 1.1,autopct = '%3.1f%%',shadow = False,
                                startangle = 90,pctdistance = 0.6)
for t in l_text:
    t.set_size(15)
for t in p_text:
    t.set_size(15)
plt.axis('equal')
plt.title('CENTRUM/善存', size=15)
plt.legend(loc=2)

ax=plt.subplot(3,4,4)
labels_4 = [u"9.5折",u"9折",u"无打折"]
sizes_4 = [266,127,10]
colors = ['orange','yellowgreen','lightskyblue','yellow']
explode = (0.05,0,0)
patches,l_text,p_text = plt.pie(sizes_4,explode=explode,labels=labels_4,colors=colors,
                                labeldistance = 1.1,autopct = '%3.1f%%',shadow = False,
                                startangle = 90,pctdistance = 0.6)
for t in l_text:
    t.set_size(15)
for t in p_text:
    t.set_size(15)
plt.axis('equal')
plt.title('elevit/爱乐维', size=15)
plt.legend(loc=2)

ax=plt.subplot(3,4,5)
labels_5 = [u"9.5折",u"9折",u"无打折"]
sizes_5= [812,216,7]
colors = ['orange','yellowgreen','lightskyblue','yellow']
explode = (0.05,0,0)
patches,l_text,p_text = plt.pie(sizes_5,explode=explode,labels=labels_5,colors=colors,
                                labeldistance = 1.1,autopct = '%3.1f%%',shadow = False,
                                startangle = 90,pctdistance = 0.6)
for t in l_text:
    t.set_size(15)
for t in p_text:
    t.set_size(15)
plt.axis('equal')
plt.title('星鲨', size=15)
plt.legend(loc=2)

ax=plt.subplot(3,4,6)
labels_6 = [u"9.5折",u"9折",u"无打折"]
sizes_6= [250,219,1]
colors = ['orange','yellowgreen','blue']
explode = (0.05,0,0)
patches,l_text,p_text = plt.pie(sizes_6,explode=explode,labels=labels_6,colors=colors,
                                labeldistance = 1.1,autopct = '%3.1f%%',shadow = False,
                                startangle = 90,pctdistance = 0.6)
for t in l_text:
    t.set_size(15)
for t in p_text:
    t.set_size(15)
plt.axis('equal')
plt.title('伊可新', size=15)
plt.legend(loc=2)

ax=plt.subplot(3,4,7)
labels_7 = [u"9折",u"9.5折",u"8.5折",u"无打折"]
sizes_7= [1434,1155,254,96]
colors = ['orange','yellowgreen','lightskyblue','yellow']
explode = (0.05,0,0,0)
patches,l_text,p_text = plt.pie(sizes_7,explode=explode,labels=labels_7,colors=colors,
                                labeldistance = 1.1,autopct = '%3.1f%%',shadow = False,
                                startangle = 90,pctdistance = 0.6)
for t in l_text:
    t.set_size(15)
for t in p_text:
    t.set_size(15)
plt.axis('equal')
plt.title('养生堂', size=15)
plt.legend(loc=2)

ax=plt.subplot(3,4,8)
labels_8 = [u"9.5折",u"9折",u"8.5折",u"无打折"]
sizes_8= [2403,2129,967,176]
colors = ['orange','yellowgreen','lightskyblue','yellow']
explode = (0.05,0,0,0)
patches,l_text,p_text = plt.pie(sizes_8,explode=explode,labels=labels_8,colors=colors,
                                labeldistance = 1.1,autopct = '%3.1f%%',shadow = False,
                                startangle = 90,pctdistance = 0.6)
for t in l_text:
    t.set_size(15)
for t in p_text:
    t.set_size(15)
plt.axis('equal')
plt.title('BY-HEALTH/汤臣倍健', size=15)
plt.legend(loc=2)

ax=plt.subplot(3,4,9)
labels_9 = [u"9折",u"9.5折",u"8.5折",u"无打折"]
sizes_9= [657,219,170,2]
colors = ['orange','yellowgreen','lightskyblue','blue']
explode = (0.05,0,0,0)
patches,l_text,p_text = plt.pie(sizes_9,explode=explode,labels=labels_9,colors=colors,
                                labeldistance = 1.1,autopct = '%3.1f%%',shadow = False,
                                startangle = 90,pctdistance = 0.6)
for t in l_text:
    t.set_size(15)
for t in p_text:
    t.set_size(15)
plt.axis('equal')
plt.title('FANCL', size=15)
plt.legend(loc=2)

ax=plt.subplot(3,4,10)
labels_10 = [u"9.5折",u"9折",u"无打折"]
sizes_10= [59,32,1]
colors = ['orange','yellowgreen','lightskyblue','yellow']
explode = (0.05,0,0)
patches,l_text,p_text = plt.pie(sizes_10,explode=explode,labels=labels_10,colors=colors,
                                labeldistance = 1.1,autopct = '%3.1f%%',shadow = False,
                                startangle = 90,pctdistance = 0.6)
for t in l_text:
    t.set_size(15)
for t in p_text:
    t.set_size(15)
plt.axis('equal')
plt.title('朗迪', size=15)
plt.legend(loc=2)
plt.show()

task 4 

Predict the total sales of Tmall's vitamin drugs in the next three months and draw a fitting curve to evaluate model performance and errors.
import pandas as pd
data=pd.read_excel('F:\\比赛\\2022全国大学生数据分析大赛\\2022全国大学生数据分析A题\\data\\new_data.xlsx')
total_month= data.groupby("date_time")["total_price"].sum().reset_index()
total_month
----------------------------------------------
    date_time	total_price
0	2020-01-01	2.907732e+07
1	2020-02-01	2.402555e+07
2	2020-03-01	3.883273e+07
3	2020-04-01	3.378211e+07
4	2020-05-01	3.248638e+07
5	2020-06-01	7.414630e+07
6	2020-07-01	3.305252e+07
7	2020-08-01	4.263468e+07
8	2020-09-01	4.516212e+07
9	2020-10-01	3.485498e+07
10	2020-11-01	4.926924e+07
11	2020-12-01	9.077518e+07
12	2021-01-01	5.548417e+07
13	2021-02-01	5.734087e+07
14	2021-03-01	7.462681e+07
15	2021-04-01	6.681211e+07
16	2021-05-01	6.643281e+07
17	2021-06-01	1.210244e+08
18	2021-07-01	6.617255e+07
19	2021-08-01	7.787211e+07
20	2021-09-01	8.814363e+07
21	2021-10-01	8.217563e+07
22	2021-11-01	8.188439e+07
23	2021-12-01	1.343489e+08
import matplotlib.pyplot as plt
plt.rcParams['font.family'] = ['sans-serif']
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.figure(figsize=(15,6))
x=["2020-01","2020-02","2020-03","2020-04","2020-05","2020-06","2020-07","2020-08","2020-09","2020-10","2020-11","2020-12",
   "2021-01","2021-02","2021-03","2021-04","2021-05","2021-06","2021-07","2021-08","2021-09","2021-10","2021-11","2021-12"]
y=total_month["total_price"]
plt.plot(x, y, color='blue')
plt.scatter(x, y, marker='o',label='真实值')
plt.xlabel('时间')
plt.xticks(x,rotation=70)
plt.ylabel('当月销售总额')
plt.title('天猫维生素类药品2020-2021月销售总额')
plt.show()

x=range(1,25)
y=total_month["total_price"]
data_1 = pd.DataFrame({'month':x,
                    'total':y})

from sklearn.model_selection import train_test_split
from sklearn.model_selection import cross_val_score

import pandas as pd

x=data_1['month']
y=data_1['total']

x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.8,random_state=420)
x_train=np.array(x_train)
x_train=x_train.reshape(-1,1)
x_test=np.array(x_test)
x_test=x_test.reshape(-1,1)

 

# 逻辑回归
from sklearn.linear_model import LogisticRegression
# k近邻回归
from sklearn.neighbors import KNeighborsRegressor
# 梯度提升回归,随机森林回归
from sklearn.ensemble import GradientBoostingRegressor,RandomForestRegressor

import numpy as np
from sklearn.metrics import r2_score,mean_absolute_error,mean_squared_error

def predict(ml_model):
    print("Model is: ", ml_model)
    
    model = ml_model.fit(x_train, y_train)
    
    print("Training score: ", model.score(x_train,y_train))
    print("Testing score: ", model.score(x_test,y_test))
    
    predictions = model.predict(x_train)
    new_predictions1=model.predict(x_test)
    print(new_predictions1)
    print(x_test1[0],model.predict([x_test1[0]]))
    print(x_test1[1],model.predict([x_test1[1]]))
    print(x_test1[2],model.predict([x_test1[2]]))
    print("Predictions: ", predictions)
    print("未来三个月: ", new_predictions1)
    print('-----------------')
    r2score = r2_score(y_train, predictions)
    print("r2 score is: ", r2score)
    
    print('MAE:{}', mean_absolute_error(y_train,predictions))
    print('MSE:{}', mean_squared_error(y_train,predictions))
    print('RMSE:{}', np.sqrt(mean_squared_error(y_train,predictions)))
predict(RandomForestRegressor(n_estimators=1))
Model is:  RandomForestRegressor(n_estimators=1)
Training score:  0.5991487172457902
Testing score:  0.42010461927846376
[33052515.35550007 33052515.35550007 45162116.98549996 45162116.98549996
 45162116.98549996 45162116.98549996 33052515.35550007 77872105.11344971
 33052515.35550007 77872105.11344971 33052515.35550007 77872105.11344971
 77872105.11344971 33052515.35550007 33052515.35550007 77872105.11344971
 77872105.11344971 77872105.11344971 45162116.98549996 77872105.11344971]
[25] [77872105.11344971]
[26] [77872105.11344971]
[27] [77872105.11344971]
Predictions:  [77872105.11344971 33052515.35550007 45162116.98549996 77872105.11344971]
Next three months: [33052515.35550007 33052515.35550007 45162116.98549996 45162116.98549996
 45162116.98549996 45162116.98549996 33052515.35550007 77872105.11344971
 33052515.35550007 77872105.11344971 33052515.35550007 77872105.11344971
 77872105.11344971 33052515.35550007 33052515.35550007 77872105.11344971
 77872105.11344971 77872105.11344971 45162116.98549996 77872105.11344971]
-----------------
r2 score is:  0.5991487172457902
MAE:{} 10788062.82364067
MSE:{} 465529197947271.7
RMSE:{} 21576125.64728134
predict(KNeighborsRegressor()) 
Model is:  KNeighborsRegressor()
Training score:  0.6370239169965028
Testing score:  0.3394163469624
[40508925.39704183 40508925.39704183 75633066.65863588 60460618.77413338
 60460618.7741334  46823311.32433338 46823311.32433337 84057002.94282563]
[25] [92884930.5029359]
[26] [92884930.5029359]
[27] [92884930.5029359]
Predictions:  [40508925.39704183 81353638.70452581 43725883.86203337 81353638.70452581
 92884930.5029359  40508925.39704183 40508925.39704183 92884930.5029359
 92884930.5029359  92884930.5029359  53784559.06508334 75633066.65863587
 87129707.09845188 43725883.86203337 46823311.32433338 84057002.94282563]
Next three months: [40508925.39704183 40508925.39704183 75633066.65863588 60460618.77413338
 60460618.7741334  46823311.32433338 46823311.32433337 84057002.94282563]
-----------------
r2 score is:  0.6370239169965028
MAE:{} 13628745.447811129
MSE:{} 322974512565174.06
RMSE:{} 17971491.662218083
predict(GradientBoostingRegressor())
Model is:  GradientBoostingRegressor()
Training score:  0.9999325937942306
Testing score:  -0.7114452499656005
[3.35533345e+07 2.92082264e+07 7.41115578e+07 4.92738241e+07]
 4.92738241e+07 4.52227237e+07 3.34846423e+07 1.20764272e+08]
[25] [1.34212591e+08]
[26] [1.34212591e+08]
[27] [1.34212591e+08]
Predictions:  [2.92082264e+07 6.68768505e+07 7.38043656e+07 6.66349711e+07
 1.34212591e+08 3.35533345e+07 3.27093976e+07 8.22873770e+07
 8.79706007e+07 8.20561062e+07 4.92738241e+07 7.41115578e+07
 7.81258889e+07 3.34846423e+07 4.52227237e+07 1.20764272e+08]
Next three months: [3.35533345e+07 2.92082264e+07 7.41115578e+07 4.92738241e+07
 4.92738241e+07 4.52227237e+07 3.34846423e+07 1.20764272e+08]
-----------------
r2 score is:  0.9999325937942306
MAE:{} 206922.37756619183
MSE:{} 59977743635.64004
RMSE:{} 244903.53945102557
x_1=range(1,28)
y_1=list(y_train)
y_1.append(1.33865662e+08)
y_1.append(1.33865662e+08)
y_1.append(1.33865662e+08)

x_2=[25,26,27]
y_2=[1.33865662e+08,1.33865662e+08,1.33865662e+08]
plt.figure(figsize=[12,6])
plot1 = plt.plot(x, y, '*',c="blue",label='原始数据')
plt.plot(x_2,y_2,'o',c='red',label='未来三个月')
plot2 = plt.plot(x_1, y_1, 'black',label='拟合曲线')
plt.xlabel("月份")
plt.xticks(x_1)
plt.ylabel('y')
plt.legend(loc=4,prop = {'size':18}) #指定legend的位置右下角
plt.title('多项式拟合')
plt.show()

task 5

A pharmaceutical company plans to sell a new vitamin brand online and hires you as a consultant to design an e-commerce business strategy of no more than two pages.

Summarize

1) The code of this competition is too complicated, and many places can be optimized

2) The overall quality of the paper has improved slightly compared with the previous participation

Finally, insert a small advertisement hhh (suitable for data analysis beginners & beginners)

(Sell papers, source programs, and supporting materials for this competition, for learning use only, for learning use only, for learning use only

Guess you like

Origin blog.csdn.net/weixin_60200880/article/details/129868183