文件内容: 总消费金额, 小费金额, 性别, 是否抽烟, 日期, 时间, 星期
,total_bill,tip,sex,smoker,day,time,size
0,16.99,1.01,Female,No,Sun,Dinner,2
1,10.34,1.66,Male,No,Sun,Dinner,3
2,21.01,3.5,Male,No,Sun,Dinner,3
3,23.68,3.31,Male,No,Sun,Dinner,2
4,24.59,3.61,Female,No,Sun,Dinner,4
5,25.29,4.71,Male,No,Sun,Dinner,4
6,8.77,2.0,Male,No,Sun,Dinner,2
7,26.88,3.12,Male,No,Sun,Dinner,4
8,15.04,1.96,Male,No,Sun,Dinner,2
9,14.78,3.23,Male,No,Sun,Dinner,2
10,10.27,1.71,Male,No,Sun,Dinner,2
11,35.26,5.0,Female,No,Sun,Dinner,4
12,15.42,1.57,Male,No,Sun,Dinner,2
13,18.43,3.0,Male,No,Sun,Dinner,4
需求:
import pandas as pd
filename = 'doc/tips.csv'
data = pd.read_csv(filename)
# 实现吸烟顾客消费金额与小费之间的散点图
smoker = data[data['smoker'] == 'Yes']
x_total_bill = smoker['total_bill']
y_tip = smoker['tip']
from pyecharts import Scatter
scatter = Scatter("吸烟顾客消费金额与小费之间的散点图")
scatter.add("", x_total_bill, y_tip)
scatter.render()
# 实现不吸烟顾客消费金额与小费之间的散点图
no_smoker = data[data['smoker'] != 'Yes']
x_total_bill = no_smoker['total_bill']
y_tip = no_smoker['tip']
from pyecharts import Scatter
scatter = Scatter("不吸烟顾客消费金额与小费之间的散点图")
scatter.add("", x_total_bill, y_tip)
scatter.render()
# 女性中吸烟与不吸烟顾客的消费金额与小费之间的散点图关系;
# 1). 获取所有吸烟的用户信息
smoker = data[data['smoker'] == 'Yes']
# 2).从所有的吸烟用户中找出性别为女的用户信息;
female_smoker = smoker[smoker['sex']=='Female']
# 1). 获取所有不吸烟的用户信息
no_smoker = data[data['smoker'] != 'Yes']
# 2).从所有的吸烟用户中找出性别为女的用户信息;
female_no_smoker = no_smoker[no_smoker['sex']=='Female']
# 3). 绘制散点图
from pyecharts import Scatter
scatter = Scatter("消费金额与小费之间的散点图")
scatter.add("吸烟女顾客", female_smoker['total_bill'], female_smoker['tip'])
scatter.add("不吸烟女顾客", female_no_smoker['total_bill'], female_no_smoker['tip'])
scatter.render()