导入包,加载数据
数据是网上获取的,数据见链接: 餐厅订单数据分析的数据 . 通过分析之后我们要用到以下三个包,分别是numpy
、pandas
、matplotlib
。加载数据我们采用pandas
中的read_excel
函数,由于该表中有三张小表,我们将这三张表都读取进来。
import numpy as np
import pandas as pd
import matplotlib. pyplot as plt
plt. rcParams[ 'font.sans-serif' ] = 'SimHei'
data1 = pd. read_excel( "E:\下载\数据分析实战项目资料\餐厅\meal_order_detail.xlsx" , sheet_name= 'meal_order_detail1' )
data2 = pd. read_excel( 'E:\下载\数据分析实战项目资料\餐厅\meal_order_detail.xlsx' , sheet_name= 'meal_order_detail2' )
data3 = pd. read_excel( 'E:\下载\数据分析实战项目资料\餐厅\meal_order_detail.xlsx' , sheet_name= 'meal_order_detail3' )
数据预处理
因为三张表不方便操作且有些内容重复,所以使用pandas
库中的concat
函数将三张表的数据拼接起来,并用dropna
方法将空的列给删掉
data = pd. concat( [ data1, data2, data3] , axis= 0 )
print ( data. head( 5 ) )
data. dropna( axis= 1 , inplace= True )
print ( data. info( ) )
分析数据–初步分析
统计卖出菜品的平均价格
print ( round ( data[ 'amounts' ] . mean( ) , 2 ) )
print ( round ( np. mean( data[ 'amounts' ] ) , 2 ) )
频数统计,最受欢迎的10
个菜,并画出菜品和销量的条形图
dishes_count = data[ 'dishes_name' ] . value_counts( ) [ : 10 ]
print ( dishes_count)
dishes_count. plot( kind= 'line' , color= [ 'r' ] )
dishes_count. plot( kind= 'bar' , fontsize= 16 )
for x, y in enumerate ( dishes_count) :
print ( x, y)
plt. text( x, y + 2 , y, ha= 'center' , fontsize= 12 )
plt. show( )
运行结果如下:
点菜的种类的top10
订单
data_group = data[ 'order_id' ] . value_counts( ) [ : 10 ]
data_group. plot( kind= 'bar' , fontsize= 16 , color= [ 'r' , 'm' , 'b' , 'y' , 'g' ] )
plt. title( '订单点菜的种类Top10' )
plt. xlabel( '订单ID' , fontsize= 16 )
plt. ylabel( '点菜种类' , fontsize= 16 )
plt. show( )
订单ID
点菜数量Top10
思路:先将id
分组,得到数量金额总数,再分组求和,将分组求和得到的结果按降序排序,对top10
画条形图。
data[ 'total_amounts' ] = data[ 'counts' ] * data[ 'amounts' ]
dataGroup = data[ [ 'order_id' , 'counts' , 'amounts' , 'total_amounts' ] ] . groupby( by= 'order_id' )
Group_sum = dataGroup. sum ( )
sort_counts = Group_sum. sort_values( by= 'counts' , ascending= False )
sort_counts[ 'counts' ] [ : 10 ] . plot( kind= 'bar' , fontsize= 16 )
plt. xlabel( '订单ID' )
plt. ylabel( '点菜数量' )
plt. title( '订单ID点菜数量Top10' )
plt. show( )
订单消费金额Top10
sort_total_amounts = Group_sum. sort_values( by= 'total_amounts' , ascending= False )
sort_total_amounts[ 'total_amounts' ] [ : 10 ] . plot( kind= 'bar' )
plt. xlabel( '订单ID' )
plt. ylabel( '消费金额' )
plt. title( '消费金额前10' )
plt. show( )
订单平均消费金额Top10
Group_sum[ 'average' ] = Group_sum[ 'total_amounts' ] / Group_sum[ 'counts' ]
sort_average = Group_sum. sort_values( by= 'average' , ascending= False )
sort_average[ 'average' ] [ : 10 ] . plot( kind= 'bar' )
plt. xlabel( '订单ID' )
plt. ylabel( '消费单价' )
plt. title( '订单消费单价前10' )
plt. show( )
订单平均消费金额Top10
Group_sum[ 'average' ] = Group_sum[ 'total_amounts' ] / Group_sum[ 'counts' ]
sort_average = Group_sum. sort_values( by= 'average' , ascending= False )
sort_average[ 'average' ] [ : 10 ] . plot( kind= 'bar' )
plt. xlabel( '订单ID' )
plt. ylabel( '消费单价' )
plt. title( '订单消费单价前10' )
plt. show( )
订单消费时间
data[ 'hourcount' ] = 1
data[ 'time' ] = pd. to_datetime( data[ 'place_order_time' ] )
data[ 'hour' ] = data[ 'time' ] . map ( lambda x: x. hour)
gp_by_hour = data. groupby( by= 'hour' ) . count( ) [ 'hourcount' ]
gp_by_hour. plot( kind= 'bar' )
plt. xlabel( '小时' )
plt. ylabel( '点菜数量' )
plt. title( '点菜数与小时的关系图' )
plt. show( )
每天订餐数量
data[ 'daycount' ] = 1
data[ 'day' ] = data[ 'time' ] . map ( lambda x: x. day)
gp_by_day = data. groupby( by= 'day' ) . count( ) [ 'daycount' ]
gp_by_day. plot( kind= 'bar' )
plt. xlabel( '8月份日期' )
plt. ylabel( '点菜数量' )
plt. title( '点菜数量与日期的关系图' )
plt. show( )
点菜量最大的前5天
gp_by_day[ : 5 ] . sort_values( ascending= False ) . plot( kind= 'bar' )
plt. xlabel( '点菜量Top5' )
plt. ylabel( '点菜数量' )
plt. title( '点菜数量与日期Top5' )
plt. show( )
查看本月周几点餐人数最多
data[ 'weekcount' ] = 1
data[ 'weekday' ] = data[ 'time' ] . map ( lambda x: x. weekday( ) )
gp_by_weekday = data. groupby( by= 'weekday' ) . count( ) [ 'weekcount' ]
gp_by_weekday. plot( kind= 'bar' )
plt. xlabel( '星期' )
plt. ylabel( '点菜数量' )
plt. title( '点菜数量与星期关系图' )
plt. show( )
查看本月周几点餐人数最多
data[ 'weekcount' ] = 1
data[ 'weekday' ] = data[ 'time' ] . map ( lambda x: x. weekday( ) )
gp_by_weekday = data. groupby( by= 'weekday' ) . count( ) [ 'weekcount' ]
gp_by_weekday. plot( kind= 'bar' )
plt. xlabel( '星期' )
plt. ylabel( '点菜数量' )
plt. title( '点菜数量与星期关系图' )
plt. show( )
进一步分析
计算每日销售额,绘制折线图分析8
月餐饮销售额趋势
data[ 'price' ] = data[ 'counts' ] * data[ 'amounts' ]
week = pd. DatetimeIndex( data[ 'place_order_time' ] )
data[ 'weekday' ] = week. day_name( )
data[ 'day' ] = pd. DatetimeIndex( data[ 'place_order_time' ] ) . day
data_gb = data[ [ 'day' , 'price' ] ] . groupby( by= 'day' )
number = data_gb. agg( np. sum )
print ( number)
plt. figure( figsize= ( 10 , 7 ) )
plt. scatter( range ( 1 , 32 ) , number, marker= 'D' )
plt. plot( range ( 1 , 32 ) , number[ 'price' ] )
plt. title( '8月餐饮销售额趋势示意图' )
plt. xlabel( '日期' )
plt. ylabel( '销售额' )
plt. xticks( range ( 0 , 32 ) [ : : 7 ] , range ( 0 , 32 ) [ : : 7 ] )
plt. show( )
运行结果如下: 分析:明显可以看出销售额有四个顶峰,说明这四个顶峰的两天可能是周末,也符合现状,在周末的销售额会更高。
更近一步分析这四个两天的时间段是不是周末。
data_gb = data[ [ 'weekday' , 'price' ] ] . groupby( by= 'weekday' )
outcome = data_gb. agg( np. sum )
print ( outcome)
sort = [ 'Monday' , 'Tuesday' , 'Wednesday' , 'Thursday' , 'Friday' , 'Saturday' , 'Sunday' ]
outcome2 = outcome. loc[ sort, 'price' ]
print ( outcome2)
plt. bar( range ( 1 , len ( outcome2) + 1 ) , outcome2, width= 0.5 , alpha= 0.5 )
plt. xticks( range ( 1 , len ( outcome2) + 1 ) , outcome2. index)
plt. title( '星期与销售额的数量情况' )
for i, j in zip ( range ( 1 , len ( outcome2) + 1 ) , outcome2) :
plt. text( i, j, '%i' % j, ha= 'center' , va= 'bottom' )
plt. show( )
运行结果如下: 分析:如图所示,销售额最好的那两天一定是周末
绘制圆环图分析星期与销售额占比情况
plt. figure( figsize= ( 5 , 5 ) )
plt. style. use( 'Solarize_Light2' )
plt. pie( outcome2, labels= outcome2. index, autopct= '%.2f %%' , wedgeprops= dict ( width= 0.6 , edgecolor= 'w' ) )
plt. title( '星期销售额占比情况' )
plt. show( )
运行结果如下: 分析:周六和周天的销售额总和占整个月的六成,由此我们可以看出餐厅的盈利基本上是靠周末。
绘制气泡图查看时间,订单量,销售额的关系
data_gb = data[ [ 'order_id' , 'price' , 'day' ] ] . groupby( by= 'day' )
def sort ( data) :
return len ( np. unique( data) )
outcome3 = data_gb. agg( { 'price' : np. sum , 'order_id' : sort} )
print ( outcome3)
plt. rcParams[ 'font.sans-serif' ] = [ 'SimHei' ]
plt. figure( figsize= ( 10 , 6 ) )
plt. scatter( range ( 1 , 32 ) , outcome3[ 'price' ] , s= outcome3[ 'order_id' ] )
plt. title( '订单量、销售额与时间的关系' )
plt. xlabel( '时间' )
plt. ylabel( '销售额' )
plt. show( )
运行结果如下: 总结:
餐厅生意最好的时候是每周的周末 点的最多的菜品是凉拌菠菜、古道小庄、麻辣小龙虾、爆炒鱿鱼和芝士烩波士顿龙虾,说明客户爱吃蔬菜,小龙虾等,可以对这类菜品进行开发,还能利用菜品信息对菜品库存进行调整减少浪费 消费金额高和频率高的顾客可以定位出来,以优惠券的形式吸引二次消费