Adventure电商自行车业务分析报告3

三、2019年11月自行车产品销售表现

3.1、数据源 dw_customer_order

#gather_customer_order在分析自行车整体表现得时已从数据库导入表( dw_customer_order),并筛选仅自行车数据,这里不再导入
#查看数据源
gather_customer_order.head(3)
image.png

3.2、细分市场销量表现

gather_customer_order表利用groupby聚合月份,求每个月自行车的销售数量,赋值给变量gather_customer_order_group_month

#求每个月自行车累计销售数量
gather_customer_order_group_month = gather_customer_order.groupby('create_year_month').agg({order_num'':'sum'}).reset_index()
#答案写法:gather_customer_order_group_month = gather_customer_order.groupby('create_year_month').order_num.sum().reset_index()
gather_customer_order_group_month.head()
image.png

利用pd.merge模块合并自行车销售信息表(gather_customer_order)+自行车每月累计销售数量表(gather_customer_order_group_month)赋值变量给order_num_proportion

#合并自行车销售信息表+自行车每月累计销售数量表,pd.merge
order_num_proportion = pd.merge(gather_customer_order,gather_customer_order_group_month,on='create_year_month')
order_num_proportion
image.png

计算自行车销量/自行车每月销量占比,计算结果形成新的列'order_proportion'

#计算自行车销量/自行车每月销量占比
order_num_proportion['order_proportion'] = order_num_proportion['order_num_x']/order_num_proportion['order_num_y']

重命名自行车每月销售量order_num_y为sum_month_order

#重命名sum_month_order:自行车每月销售量
order_num_proportion = order_num_proportion.rename(columns={'order_num_y':'sum_month_order'})
order_num_proportion.head()
image.png

将最终的order_num_proportion的DataFrame存入Mysql的ppt_bicycle_product_sales_month_4当中,请使用追加存储。

#将每月自行车销售信息存入数据库
engine = create_engine('mysql://用户名:密码@ip/数据库实例?charset=gbk')
order_num_proportion.to_sql('pt_bicycle_product_sales_month_4_Effy',con = engine,if_exists='replace', index=False)

3.3、公路/山地/旅游自行车细分市场表现

查看cpzl_zw有哪些产品子类

#查看自行车有那些产品子类
gather_customer_order['cpzl_zw'].unique()
#答案:gather_customer_order['cpzl_zw'].drop_duplicates()
image.png

公路自行车细分市场销量表现

gather_customer_order_road = gather_customer_order[gather_customer_order['cpzl_zw'] == '公路自行车']
#求公路自行车不同型号产品销售数量
gather_customer_order_road_month = gather_customer_order_road.groupby(by = ['create_year_month','product_name']).agg({'order_num':'sum'}).reset_index()
gather_customer_order_road_month
image.png
#新增一列将上表标记为‘公路自行车’
gather_customer_order_road_month['cpzl_zw'] = '公路自行车'

求每个月公路自行车累计销售数量 赋值为gather_customer_order_road_month_sum,记得重置索引

#每个月公路自行车累计销售数量
gather_customer_order_road_month_sum = gather_customer_order_road_month.groupby('create_year_month').order_sum.sum().reset_index()
gather_customer_order_road_month_sum.head()
image.png

在gather_customer_order_road_month基础上,合并公路自行车每月累计销售数量gather_customer_order_road_month_sum,主键为'create_year_month'

#合并公路自行车gather_customer_order_road_month与每月累计销售数量
#用于计算不同型号产品的占比
gather_customer_order_road_month = pd.merge(gather_customer_order_road_month,gather_customer_order_road_month_sum,on='create_year_month')
gather_customer_order_road_month
image.png

山地自行车
与公路自行车处理过程一致,赋予变量gather_customer_order_Mountain筛选山地自行车→求山地自行车不同型号的产品销售数量→求每月累计销售数量→合并→目的是用于产品子类比较环比

# 筛选
gather_customer_order_Mountain = gather_customer_order[gather_customer_order['cpzl_zw'] == '山地自行车']
#求山地自行车不同型号产品销售数量
gather_customer_order_Mountain_month = gather_customer_order_Mountain.groupby(by=['create_year_month','product_name']).order_num.sum().reset_Index()
gather_customer_order_Mountain_month['cpzl_zw'] = '山地自行车'
#每个月公路自行车累计销售数量
gather_customer_order_Mountain_month_sum = gather_customer_order_Mountain_month.groupby('create_year_month').order_num.sum().reset_Index()

#合并山地自行车hz_customer_order_Mountain_month与每月累计销售数量
#用于计算不同型号产品的占比
gather_customer_order_Mountain_month = pd.merge(gather_customer_order_Mountain_month,gather_customer_order_Mountain_month_sum,on='create_year_month')
gather_customer_order_Mountain_month
image.png

旅游自行车
与公路自行车处理过程一致,赋予变量gather_customer_order_tour,筛选旅游自行车→求旅游自行车不同型号的产品销售数量→求每月累计销售数量→合并→目的是用于产品子类比较环比

gather_customer_order_tour = gather_customer_order[gather_customer_order['cpzl_zw'] == '旅游自行车']
#求旅游自行车不同型号产品销售数量
gather_customer_order_tour_month = gather_customer_order_tour.groupby(by = ['create_year_month','product_name']).order_num.sum().reset_index()
gather_customer_order_tour_month['cpzl_zw'] = '旅游自行车'
gather_customer_order_tour_month_sum =  gather_customer_order_tour_month.groupby('create_year_month').order_num.sum().reset_index()
gather_customer_order_tour_month = pd.merge(gather_customer_order_tour_month,gather_customer_order_tour_month_sum,on='create_year_month')
gather_customer_order_tour_month
image.png
#将山地自行车、旅游自行车、公路自行车每月销量信息合并
gather_customer_order_month = pd.concat([gather_customer_order_road_month,gather_customer_order_Mountain_month,gather_customer_order_tour_month])

新增一列'order_num_proportio',为销售量占每月自行车总销售量比率

#各类自行车,销售量占每月自行车总销售量比率
gather_customer_order_month['order_num_proportio'] = gather_customer_order_month['order_num_x']/gather_customer_order_month['order_num_y']
gather_customer_order_month
image.png

gather_customer_order_month中的order_num_x(当月产品累积销量)修改字段名为order_month_product, order_num_y(当月自行车总销量)修改字段名为sum_order_month

#order_month_product当月产品累计销量
#sum_order_month当月自行车总销量
gather_customer_order_month.rename(columns={'order_num_x':'order_month_product','order_num_y':'sum_order_month'})
gather_customer_order_month
image.png
#将数据存入数据库
engine =create_engine('mysql://用户名:密码@ip/数据库实例?charset=gbk') 
gather_customer_order_month.to_sql('pt_bicycle_product_sales_order_month_4_Effy',con = datafrog,if_exists='replace', index=False)

计算2019年11月自行车环比

计算11月环比,先筛选10月11月数据
gather_customer_order_month_10_11 = gather_customer_order_month[gather_customer_order_month.create_year_month.isin(['2019-10','2019-11'])]
#排序。将10月11月自行车销售信息排序
gather_customer_order_month_10_11 = gather_customer_order_month_10_11.sort_values(by = ['product_name','create_year_month'])
gather_customer_order_month_10_11.head(3)
image.png
product_name  = list(gather_customer_order_month_10_11.product_name.drop_duplicates())
product_name 
image.png

计算自行车销售数量环比

#计算自行车销售数量环比
product_name_list = list(gather_customer_order_month_10_11.product_name.drop_duplicates())# 不懂为什么还要list一遍
order_top_x=pd.Series([])
for i in product_name:
    a=gather_customer_order_month_10_11.loc[gather_customer_order_month_10_11['product_name']==i]['order_month_product'].pict_change().fillna(0)
    order_top_x=order_top_x.append(a)
gather_customer_order_month_10_11['order_num_diff'] = order_top_x
gather_customer_order_month_10_11.head()
image.png
#筛选出11月自行车数据,10月份的销量环比是0,只需要看11月环比
gather_customer_order_month_11 = gather_customer_order_month_10_11[gather_customer_order_month_10_11['create_year_month'] == '2019-11']
gather_customer_order_month_11.head(3)
image.png

计算2019年1月至11月产品累计销量
筛选2019年1月至11月自行车数据,赋予变量为gather_customer_order_month_1_11

#筛选2019年1月至11月自行车数据,这里使用的是‘~’取对立面
gather_customer_order_month_1_11 = gather_customer_order_month[gather_customer_order_month['create_year_month'].isin(['2019-01','2019-02','2019-03','2019-04','2019-05','2019-06','2019-07','2019-08','2019-09','2019-10','2019-11'])]
gather_customer_order_month_1_11.head()
image.png
#计算2019年1月至11月自行车累计销量
gather_customer_order_month_1_11_sum = gather_customer_order_month_1_11.groupby(by = 'product_name').order_month_product.sum().reset_index()
#重命名sum_order_1_11:1-11月产品累计销量
gather_customer_order_month_1_11_sum = gather_customer_order_month_1_11_sum.rename(columns = {'order_month_product':'sum_order_1_11'})
gather_customer_order_month_1_11_sum.head()
image.png

2019年11月自行车产品销量、环比、累计销量
累计销量我们在gather_customer_order_month_1_11_sum中已计算好,11月自行车环比、及产品销量占比在gather_customer_order_month_11已计算好,这里我们只需
将两张表关联起来,用pd.merge()

#观察表数据
gather_customer_order_month_11.head(3)
image.png
#观察表数据
gather_customer_order_month_1_11_sum.head(3)
image.png

以gather_customer_order_month_11为主,合并gather_customer_order_month_1_11_sum表,主键为product_name

#按相同字段product_name产品名,合并两张表
gather_customer_order_month_11 = pd.merge(gather_customer_order_month_11,gather_customer_order_month_1_11_sum,on='product_name')
gather_customer_order_month_11.head()
image.png

字段注释:
create_year_month:时间;product_name:产品,order_month_product:产品本月累计销量,cpzl_zw:产品类别,sum_order_month:当月自行车总销量,
order_num_proportio:产品本月占比,order_num_diff:产品本月环比,sum_order_1_11:1-11月产品累计销量

将最终的gather_customer_order_month_11的DataFrame存入Mysql的pt_bicycle_product_sales_order_month_11当中,请使用追加存储。

#存入数据库
engine = create_engine('mysql://用户名:密码@ip/数据库实例?charset=gbk')
gather_customer_order_month_11.to_sql('pt_bicycle_product_sales_order_month_11_Effy',,con = engine,if_exists='replace', index=False)

你可能感兴趣的:(Adventure电商自行车业务分析报告3)