自行车业务分析 2.2

2020-06-09  本文已影响0人  人间桑

2.2、2019年11月自行车区域销售量表现

作业 2.2.1 请按照'chinese_territory','create_year_month',区域、月份分组,订单量求和、销售金额求和,赋予变量gather_customer_order_10_11_group,记得重置索引

#按照区域、月分组,订单量求和,销售金额求和

gather_customer_order_10_11_group=gather_customer_order_10_11.groupby(['chinese_territory','create_year_month']).agg( {'order_num':'sum','sum_amount':'sum'},axis=0).reset_index()

#记得加中括号[]

gather_customer_order_10_11_group.head()

作业 2.2.2 求不同区域10月11月的环比 你需要一步步完成

提示

1、获得去重区域的列表region_list

2、利用for循环区域列表,结合loc定位符合区域

3、利用pct_change()函数实现环比效果

4、形成新的Series

5、利用Series带的索引,合并到gather_customer_order_10_11_group中

#将区域存为列表

region_list=list(gather_customer_order_10_11_group['chinese_territory'].drop_duplicates(keep='first'))

#pct_change()当前元素与先前元素的相差百分比,求不同区域10月11月环比

order_x = pd.Series([])

amount_x = pd.Series([])

for i in region_list:

a=gather_customer_order_10_11_group.loc[gather_customer_order_10_11_group['chinese_territory']==i]['order_num'].pct_change().fillna(0)

b=gather_customer_order_10_11_group.loc[gather_customer_order_10_11_group['chinese_territory']==i]['sum_amount'].pct_change().fillna(0)

order_x=order_x.append(a)

amount_x = amount_x.append(b)

gather_customer_order_10_11_group['order_diff']=order_x

gather_customer_order_10_11_group['amount_diff']=amount_x

gather_customer_order_10_11_group.head()

作业 2.2.3、将最终的gather_customer_order_10_11_group的DataFrame存入Mysql的pt_bicy_november_territory_2当中,请使用追加存储。

注意:存储的MYSQL账号名为frogdata05

#将数据存入数据库

engine = create_engine("mysql://frogdata05:Frogdata!123@106.15.121.232:3306/datafrog05_adventure?charset=utf8")

datafrog05 = engine

gather_customer_order_10_11_group.to_sql('pt_bicy_november_territory_2',con=datafrog05,if_exists='append')

上一篇 下一篇

猜你喜欢

热点阅读