1.导入模块
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签
plt.rcParams['axes.unicode_minus']=False #用来正常显示负号
%matplotlib inline
2.获取数据
fdata=pd.read_excel("F:/专业课程作业/python时空数据分析与可视化/tips.xls")
fdata.head()

3.分析数据
# (1)查看数据的描述信息
fdata.describe()

# (2)修改列名为汉字,并显示前5行
fdata.rename(columns={"total_bill":"消费总额","tip":"小费","sex":"性别","smoker":"是否吸烟",\
"day":"星期","time":"聚餐时间段","size":"人数"},inplace=True)
fdata.head()

# (3)求人均消费
fdata['人均消费']=round(fdata['消费总额']/fdata['人数'],2)
fdata.head()

# (4)查询吸烟男性中人均消费大于15的数据
# 方法1
fdata[(fdata['是否吸烟']=='Yes')&(fdata['性别']=='Male')&(fdata['人均消费']>15)]
# 方法2
fdata[(fdata.是否吸烟=='Yes')&(fdata.性别=='Male')&(fdata.人均消费>15)]
# 方法3
fdata.query("是否吸烟=='Yes'&性别=='Male'&人均消费>15")

# (5)分析小费总额与消费总额的关系
fdata.plot(kind='scatter',x='小费',y='消费总额')

# (6)分析男性顾客和女性顾客谁更慷慨
fdata.groupby(['性别'])['小费'].mean()

# (7)分析星期和消费的关系
# 显示星期的取值
print(fdata['星期'].unique())
r=fdata.groupby(['星期'])['小费'].mean()
fig=r.plot(kind="bar",x='星期',y='小费',fontsize=12,rot=30)
fig.axes.title.set_size(16)

# (8)性别+吸烟的组合对慷慨度的影响
s=fdata.groupby(['性别','是否吸烟'])['小费'].mean()
s.plot(kind='bar',x=['性别','是否吸烟'],y='小费',rot=30)

# (9)分析聚餐时间段与小费数额的关系
f=fdata.groupby(['聚餐时间段'])['小费'].mean()
f.plot(kind="barh",x='聚餐时间段',y='小费')
