一、自行车整体销售表现
1.1、从数据库读取源数据:dw_customer_order
作业 1.1.1、从Mysql读取dw_customer_order,形成DataFrame格式,赋予变量gather_customer_order,注意Mysql账号是有adventure_ods权限
#读取源数据。不同城市,每天产品销售信息
#创建数据库引擎
engine = create_engine("mysql://frogdata001:Frogdata!123@106.13.128.83:3306/adventure_ods?charset=utf8")
gather_customer_order=pd.read_sql_query('select * from dw_customer_order',con=engine)
#设定对应数据库的引擎,然后引用引擎
#查看源数据前5行,观察数据,判断数据是否正常识别
gather_customer_order.head()

作业 1.1.2、查看源数据类型:dw_customer_order
gather_customer_order.info()

作业 1.1.3、利用create_date字段增加create_year_month月份字段
#查看数据类型
gather_customer_order['create_date'][0]
#增加create_year_month月份字段。按月维度分析时使用
gather_customer_order['create_year_month']=gather_customer_order.create_date.apply(lambda x:x.strftime('%Y-%m'))
gather_customer_order['create_year_month'].head()

#注意strftime用法
#关于时间与字符串的相互转换:https://www.cnblogs.com/strivepy/p/10436213.html
https://www.csdn.net/gather_2c/MtjaMg5sNTM0LWJsb2cO0O0O.html
作业 1.1.4、筛选产品类型cplb_zw中的自行车作为新的gather_customer_order,目前存在如图情况,你需要剔除掉其余2个

#筛选产品类别为自行车的数据
gather_customer_order = gather_customer_order[gather_customer_order['cplb_zw']=='自行车']
gather_customer_order.head()

网友评论