小费&消费&pandas可视化

小费数据集来源python第三方库seaborn中自带数据;
一:数据导入
import numpy as np
from pandas import Series,DataFrame
import pandas as pd
import seaborn as sns  #导入seaborn库中自带数据

In [6]:

tips = sns.load_dataset('tips')
tips.head()

Out[6]:

 total_billtipsexsmokerdaytimesize
016.991.01FemaleNoSunDinner2
110.341.66MaleNoSunDinner3
221.013.50MaleNoSunDinner3
323.683.31MaleNoSunDinner2
424.593.61FemaleNoSunDinner4

In [7]:

二:定义问题:

三:数据清洗:

tips.shape#查看数据集的大小

Out[7]:

(244, 7)

In [8]:

tips.describe()#查看数据集中各属性

Out[8]:

 total_billtipsize
count

 

 

244.000000

244.000000244.000000
mean19.7859432.9982792.569672
std8.9024121.3836380.951100
min3.0700001.0000001.000000
25%13.3475002.0000002.000000
50%17.7950002.9000002.000000
75%24.1275003.5625003.000000
max50.81000010.0000006.000000

In [9]:

tips.info()#查看数据集是否有缺失值
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 244 entries, 0 to 243
Data columns (total 7 columns):
total_bill    244 non-null float64
tip           244 non-null float64
sex           244 non-null category
smoker        244 non-null category
day           244 non-null category
time          244 non-null category
size          244 non-null int64
dtypes: category(4), float64(2), int64(1)
memory usage: 7.2 KB

In [10]:

四:数据探索:

#散点图用来表示数据之间的规律 通过plot函数的kind = 'scatter'可进行绘制;
tips.plot(kind = 'scatter',x = 'total_bill',y = 'tip')
#小费金额与消费总额进行分析,看看之间的关联(绘制散点图)

Out[10]:

<matplotlib.axes._subplots.AxesSubplot at 0x2470bc30be0>

In [42]:

#线性图
#线性图用于绘制两组数据之间的趋势;plot()方法
tips.plot(x = 'total_bill',y = 'tip')

Out[42]:

<matplotlib.axes._subplots.AxesSubplot at 0x2470c727dd8>

In [11]:

#以下代码为性别与小费关系(使用柱状图) 
#首先通过sex属性名计算不通属性值的平均值
male_tip = tips[tips['sex'] == 'Male']['tip'].mean()
male_tip

Out[11]:

3.0896178343949052

In [14]:

female_tip = tips[tips['sex'] == 'Female']['tip'].mean()
female_tip

Out[14]:

2.833448275862069

In [15]:

#Series 是一个一维数组对象 ,它包含一组索引和一组数据,可以把它理解为一组带索引的数组。
s = Series([male_tip,female_tip],index=['male','female'])
s

Out[15]:

male      3.089618
female    2.833448
dtype: float64

In [16]:

#柱状图
#通过plot函数的kind = 'bar'可进行绘制
s.plot(kind='bar')

Out[16]:

<matplotlib.axes._subplots.AxesSubplot at 0x2470c209278>

 

In [40]:

#水平柱状图(类别较多情况)
#通过plot函数的kind = 'barh'可进行绘制
s.plot(kind='barh')

Out[40]:

<matplotlib.axes._subplots.AxesSubplot at 0x2470c6569b0>

In [41]:

#堆积柱状图(类别较多情况)
#通过plot函数的kind = 'barh'可进行绘制再加stacked参数设置
s.plot(kind='barh',stacked=True,alpha=0.5)

Out[41]:

<matplotlib.axes._subplots.AxesSubplot at 0x2470c6d1b00>

In [39]:

#通过unique函数查看属性列下的唯一值
#查看日期的唯一值
tips['day'].unique()

Out[39]:

[Sun, Sat, Thur, Fri]
Categories (4, object): [Sun, Sat, Thur, Fri]

In [24]:

#以下为日期平均小费柱状图构成
Sun_tip = tips[tips['day'] == 'Sun']['tip'].mean()
Sun_tip

Out[24]:

3.255131578947369

In [27]:

Sat_tip = tips[tips['day'] == 'Sat']['tip'].mean()
Sat_tip

Out[27]:

2.993103448275862

In [26]:

Thur_tip = tips[tips['day'] == 'Thur']['tip'].mean()
Thur_tip

Out[26]:

2.771451612903226

In [25]:

Fri_tip = tips[tips['day'] == 'Fri']['tip'].mean()
Fri_tip

Out[25]:

2.734736842105263

In [30]:

day_tip = Series([Sun,Sat,Thur,Fri],index=['Sun_tip','Sat_tip','Thur_tip','Fri_tip'])
day_tip

Out[30]:

Sun_tip     3.255132
Sat_tip     2.993103
Thur_tip    2.771452
Fri_tip     2.734737
dtype: float64

In [31]:

day_tip.plot(kind='bar')

Out[31]:

<matplotlib.axes._subplots.AxesSubplot at 0x2470c261eb8>

In [32]:

#小费百分比6
tips['percent_tip'] = tips['tip']/(tips['total_bill']+tips['tip'])
tips.head(8)

Out[32]:

 total_billtipsexsmokerdaytimesizepercent_tip
016.991.01FemaleNoSunDinner20.056111
110.341.66MaleNoSunDinner30.138333
221.013.50MaleNoSunDinner30.142799
323.683.31MaleNoSunDinner20.122638
424.593.61FemaleNoSunDinner40.128014
525.294.71MaleNoSunDinner40.157000
68.772.00MaleNoSunDinner20.185701
726.883.12MaleNoSunDinner40.104000

In [33]:

#密度图
#核密度估计(将数据的分布近似为一组核)(正态分布)
tips['percent_tip'].plot(kind='kde')
#通过plot函数的kind='kde'可进行绘制;

Out[33]:

<matplotlib.axes._subplots.AxesSubplot at 0x2470c2cdbe0>

In [38]:

#直方图
#直方图可用于频率分布,y轴可为数值或者比率(可以看出大概分布规律)
#通过hist方法绘制直方图(bin参数将值分为多少段默认为10,grid参数可图表中添加网格)
tips['tip'].hist(bins=10,grid=False)

Out[38]:

<matplotlib.axes._subplots.AxesSubplot at 0x2470c518358>

  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值