Python数据分析实战,小费数据集应用
一、数据来源
本节选用的是Python的第三方库seaborn自带的数据集,该小费数据集为餐饮行业收集的数据,其中total_bill为消费总金额、tip为小费金额、sex为顾客性别、smoker为顾客是否吸烟、day为消费的星期、time为聚餐的时间段、size为聚餐人数。
import numpy as np from pandas import Series,DataFrame import pandas as pd import seaborn as sns #导入seaborn库 tips=sns.load_dataset('tips') #seaborn库自带的数据集 tips.head()

二、问题探索
- 小费金额与消费总金额是否存在相关性?
- 性别、是否吸烟、星期几、聚餐人数和小费金额是否有一定的关联?
- 小费金额占小费总金额的百分比是否服从正态分布?
三、数据清洗
tips.shape #数据集的维度
(244,7)
共有244条数据,7列。
tips.describe() #描述统计

描述统计结果如上所示。
tips.info() #查看缺失值信息

此例无缺失值。
四、数据探索
tips.plot(kind='scatter',x='total_bill',y='tip') #绘制散点图

由图可看出,小费金额与消费总金额存在正相关性。
import numpy as np from pandas import Series,DataFrame import pandas as pd import seaborn as sns #导入seaborn库 tips=sns.load_dataset('tips')#seaborn库自带的数据集 tips.head()
3.0896178343949052
female_tip = tips[tips['sex'] == 'Female']['tip'].mean() #女性平均消费金额female_tip
2.833448275862069
s = Series([male_tip,female_tip],index=['male','female']) s
male 3.089618
female 2.833448
dtype: float64
s.plot(kind='bar') #男女平均小费柱状图

由图可看出,女性小费金额小于男性小费金额。
sun_tip = tips[tips['day'] == 'Sun']['tip'].mean() sat_tip = tips[tips['day'] == 'Sat']['tip'].mean() thur_tip = tips[tips['day'] == 'Thur']['tip'].mean() fri_tip = tips[tips['day'] == 'Fri']['tip'].mean()#各个日期的平均小费值 s = Series([thur_tip,fri_tip,sat_tip,sun_tip],index=['Thur','Fri','Sat','Sun']) s

s.plot(kind='bar') #日期平均小费柱状图

由图可看出,周六、周日的小费比周四、周五的小费高。
tips['percent_tip'] = tips['tip']/(tips['total_bill']+tips['tip']) tips.head(10) #小费所占百分比

相关推荐
茄肥猫的窝 2020-10-29
kkbb 2020-10-27
gallon00 2020-10-16
Cocainebai 2020-08-15
flyfor0 2020-11-16
aiwozhiai 2020-10-06
HashData0 2020-09-18
GooTal 2020-09-16
qiujiahao 2020-09-15
Dimples 2020-09-15
qiujiahao 2020-09-11
wangquannuaa 2020-08-30
逍遥友 2020-08-21
nxcjh 2020-08-17
CodeAsWind 2020-08-17
zlfing 2020-08-16
wordmhg 2020-08-16
子昊的茶会 2020-08-04
天涯莺歌 2020-08-03