任务
1.按 “时间” 对订单经行 数据拆分
import pandas as pd
import numpy as np
from sqlalchemy import create_engine
engine = create_engine('mysql+pymysql://root:981221@localhost/testdb?charset=utf8mb4')
detail = pd.read_sql_table('meal_order_detail1',con=engine)
detail['place_order_time'] = pd.to_datetime(detail['place_order_time'])
detail['date'] = [i.date() for i in detail['place_order_time']]
detailGroup = detail[['date','counts','amounts']].groupby(by = 'date')
print('订单前 5 组的数目:\n',detailGroup.size().head())
订单前 5 组的数目:date
2016-08-01 217
2016-08-02 138
2016-08-03 157
2016-08-04 144
2016-08-05 193
dtype: int64
2.agg 聚合数据
dayMean = detailGroup.agg({'amounts':np.mean})
print('订单前 5 组的售价的均值:\n',dayMean.head())
订单前 5 组的售价的均值:amounts
date
2016-08-01 43.161290
2016-08-02 44.384058
2016-08-03 43.885350
2016-08-04 52.423611
2016-08-05 44.927461
dayMedian = detailGroup.agg({'amounts':np.median})
print('订单前 5 组的售价的中位数:\n',dayMedian.head())
订单前 5 组的售价的中位数:amounts
date
2016-08-01 33.0
2016-08-02 35.0
2016-08-03 38.0
2016-08-04 39.0
2016-08-05 37.0
3. apply 聚合数据 统计总数
daySaleSum = detailGroup.apply(np.sum)
print('订单前 5 组的销量的总数:\n',daySaleSum['counts'].head())
订单前 5 组的销量的总数:date
2016-08-01 233.0
2016-08-02 151.0
2016-08-03 192.0
2016-08-04 169.0
2016-08-05 224.0
Name: counts, dtype: float64