【数据分析&数据挖掘】计算每日营业额&最火菜品统计

 1 import pandas as pd
 2 import numpy as np
 3 
 4 # 计算每日营业额 加载数据
 5 detail = pd.read_excel("./meal_order_detail.xlsx")
 6 print("detail: \n", detail)
 7 print("detail的列名称: \n", detail.columns)
 8 
 9 # 1、计算出每个产品的营业额
10 detail.loc[:, "pay"] = detail.loc[:, "counts"] * detail.loc[:, "amounts"]
11 print(detail)
12 
13 # 2、构建日数据
14 # 先将时间数据转化为pandas默认支持的时间序列数据
15 detail.loc[:, "place_order_time"] = pd.to_datetime(detail.loc[:, "place_order_time"])
16 
17 # 通过列表推导式来获取日属性
18 detail.loc[:, "day"] = [i.day for i in detail.loc[:, "place_order_time"]]
19 
20 print(detail)
21 
22 # 3、计算营业额——按照日进行分组,统计pay的sum
23 res = detail.groupby(by="day")["pay"].sum()
24 # res = detail.groupby(by="day")["pay"]  # 一个对象
25 
26 print("res: \n", res)
 1 # 求这家店最火的菜品,以及售卖份数
 2 import pandas as pd
 3 
 4 # 加载数据
 5 detail = pd.read_excel("./meal_order_detail.xlsx")
 6 print("detail: \n", detail)
 7 print("detail的列名称: \n", detail.columns)
 8 
 9 # 先将dishes_name转化为category类型
10 detail.loc[:, "dishes_name"] = detail.loc[:, "dishes_name"].astype("category")
11 
12 # 统计describe分析
13 print("对于菜品最火及售卖份数的统计分析: \n", detail.loc[:, "dishes_name"].describe())
14 
15 # 白饭/大碗不算菜品
16 # 删除白饭的行
17 bool_index = detail.loc[:, "dishes_name"] == "白饭/大碗"
18 print("bool_index")
19 
20 # 确定哪些行是白饭大碗
21 drop_index_list = detail.loc[bool_index, :].index
22 
23 # 删除
24 detail.drop(labels=drop_index_list, axis=0, inplace=True)
25 
26 # 保留法
27 bool_id = detail.loc[:, "dishes_name"] != "白饭/大碗"
28 
29 # 保留True的行
30 detail = detail.loc[bool_id, :]
31 
32 # 再去统计菜品的deccribe
33 
34 # 先将dishes_name转化为category类型
35 detail.loc[:, "dishes_name"] = detail.loc[:, "dishes_name"].astype("category")
36 
37 # 统计describe分析
38 print("对于菜品最火以及售卖份数的统计分析: \n", detail)

猜你喜欢

转载自www.cnblogs.com/Tree0108/p/12116079.html