0
点赞
收藏
分享

微信扫一扫

Python实战—星巴克店铺分布数据分析

Python实战—星巴克店铺分布数据分析_缺失值

第一次吃星巴克

是一块小小的蛋糕

类似圣诞帽似的圆锥形

35块钱一块,感觉很贵

那是别人买给我的

Python实战—星巴克店铺分布数据分析_python_03

以此为背景

研究星巴克店铺的分布情况

熟悉星巴克店铺的地理分布位置


Python实战—星巴克店铺分布数据分析_python_04


一、数据来源




本节使用星巴克店铺的数据集,通过Python可视化技术,分析星巴克店铺的分布情况,使用的数据来源于网络,其中City为店铺所在城市、State/Province为店铺所在的州和省份、Country为店铺所在国家。

import numpy as npimport pandas as pdimport matplotlib.pyplot as plt%matplotlib inline

starbucks = pd.read_csv(open('directory.csv',encoding='utf-8'))starbucks.head()

Python实战—星巴克店铺分布数据分析_python_05

二、问题探索




  • 星巴克店铺在全球的分布情况
  • 哪些国家星巴克店铺较多
  • 哪些城市星巴克店铺较多
  • 星巴克店铺在我国的分布情况


三、数据清洗




starbucks.isnull().sum()

Python实战—星巴克店铺分布数据分析_python_06

查看缺失值,City列有15个缺失值。

starbucks[starbucks['City'].isnull()]  #查看缺失值的具体情况。

Python实战—星巴克店铺分布数据分析_数据_07

def fill_na(x):
return x #定义填充函数

starbucks['City'] = starbucks['City'].fillna(fill_na(starbucks['State/Province']))
starbucks[starbucks['Country']=='EG']

Python实战—星巴克店铺分布数据分析_缺失值_08

用State/Province进行缺失值的填充。

starbucks['Brand'].unique() #过滤数据
array(['Starbucks', 'Teavana', 'Evolution Fresh', 'Coffee House Holdings'], dtype=object)

new_data = starbucks[starbucks['Brand'] == 'Starbucks']
new_data['Brand'].unique() #过滤数据
array(['Starbucks'], dtype=object)

new_data.to_csv('starbucks.csv',index=False,encoding='utf-8') #保存数


四、数据探索




starbucks = pd.read_csv(open('starbucks.csv',encoding='utf-8'))
starbucks.head()

Python实战—星巴克店铺分布数据分析_缺失值_09

starbucks.shape

(25247, 13)

星巴克店铺共有25247家分店。

len(starbucks['Country'].unique())

72

分布在72个国家。

len(starbucks['City'].unique())

5405

分布在5405个城市。

country_count = starbucks['Country'].value_counts()[0:10]
country_count

Python实战—星巴克店铺分布数据分析_缺失值_10

对Country计数,筛选出店铺数量排名前10位的国家。

plt.style.use('ggplot')    #设置图表样式
labels = list(country_count.index) #刻度标签
plt.xlabel('Country') #设置X轴标签
plt.ylabel('Count') #设置Y轴标签
plt.title('Country Top 10')
plt.bar(range(len(labels)),country_count)
plt.xticks(range(len(labels)),labels,fontsize=12) #设置刻度和刻度标签

Python实战—星巴克店铺分布数据分析_缺失值_11

星巴克分布情况,美国位居榜首,中国次之。

city_count = starbucks['City'].value_counts()[0:10]
city_count

Python实战—星巴克店铺分布数据分析_数据_12

对City计数,筛选出店铺数量排名前10位的城市。

plt.figure(figsize=(10,6))  #设置图片大小
plt.rcParams['font.sans-serif'] = ['simhei'] #指定默认字体
plt.rcParams['axes.unicode_minus'] = False #解决保存图像是负号'-'显示为方块的问题
labels = list(city_count.index) #刻度标签
plt.xlabel('City') #设置X轴标签
plt.ylabel('Count') #设置Y轴标签
plt.title('City Top 10')
plt.bar(range(len(labels)),city_count)
plt.xticks(range(len(labels)),labels) #设置刻度和刻度标签

Python实战—星巴克店铺分布数据分析_python_13

上海市作为国际化大都市,星巴克店铺数量最多,西雅图作为星巴克的总部城市,排在第十位。

ownership_count = starbucks['Ownership Type'].value_counts()[0:10]
ownership_count

Python实战—星巴克店铺分布数据分析_缺失值_14

china_data = starbucks[starbucks['Country'] == 'CN'] #筛选中国的数据
china_data.head()

Python实战—星巴克店铺分布数据分析_数据_15

china_data.to_csv('cn_starbucks.csv',index=False,encoding='utf-8') #保存中国的数据

cn_starbucks = pd.read_csv(open('cn_starbucks.csv',encoding='utf-8'))
cn_starbucks.head()

Python实战—星巴克店铺分布数据分析_缺失值_16

city_count = cn_starbucks['City'].value_counts()[0:10]
city_count

Python实战—星巴克店铺分布数据分析_数据_17

对City计数,筛选出店铺数量在中国排名前10位的城市。

plt.rcParams['font.sans-serif'] = ['simhei'] #指定默认字体
plt.rcParams['axes.unicode_minus'] = False #解决保存图像是负号'-'显示为方块的问题
labels = list(city_count.index) #刻度标签
plt.xlabel('City') #设置X轴标签
plt.ylabel('Count') #设置Y轴标签
plt.title('星巴克各城市分布')
plt.barh(range(len(labels)),city_count)
plt.yticks(range(len(labels)),labels) #设置刻度和刻度标签

Python实战—星巴克店铺分布数据分析_数据_18

在中国,北上广深这四个城市的店铺排名靠前,与当地的经济实力有着密切的关系。

Python实战—星巴克店铺分布数据分析_python_04

以上就是今天推送的文章

研究星巴克店铺的分布情况

数据分析就是将实际生活问题

数据处理并且可视化的过程

一起学习吧!

Python实战—星巴克店铺分布数据分析_缺失值_20

请帮我点点在看,点点广告

Python实战—星巴克店铺分布数据分析_数据_22

Python实战—星巴克店铺分布数据分析_数据_23

举报

相关推荐

0 条评论