Python统计全球星巴克门店的数据及在不同国家和地区门店数量可视化(超详细 附源码)

需要源码和数据集请点赞关注收藏后评论区留言私信~~~

下面对一组关于全球星巴克门店的统计数据,分析了在不同国家和地区以及中国不同城市的星巴克门店的数量

1:导入模块

import pandas as pd
import numpy as np
from pandas import Series,DataFrame
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei'] #用来正常显示中文标签
plt.rcParams['axes.unicode_minus']=False #用来正常显示负号
%matplotlib inline

2:获取数据 并打印前五行

starbucks = pd.read_csv("data//directory.csv")
starbucks.head()

 3:数据分析及可视化

首先查看星巴克旗下有哪些品牌,如果我们只关心星巴克咖啡门店,则只需要获取星巴克中Brand的数据集,并查看全世界一共有多少家星巴克门门店

# 星巴克旗下有哪些品牌?
print("星巴克旗下品牌有:\n",starbucks.Brand.value_counts())
# 把重心放在星巴克的咖啡门店上面,所以只查看Brand是Starbucks的数据集内容。
coffee = starbucks[starbucks.Brand=='Starbucks']
# 全世界一共有多少家星巴克门店?
print("-------------------------")
print(coffee.shape)

 然后查看全世界一共有多少个国家和地区开设了星巴克门店,显示门店数量排名前十和后十的国家和地区

 

df = starbucks.groupby(["Country"]).size()
print("全世界一共多少个国家开设了星巴克门店:",df.size)
df1 = df.sort_values( ascending=False)
print("排名前10的国家:\n",df1.head(10))
# 星巴克门店数排名后10的国家
# df2 = df.sort_values()
# df2.head(10)
print("排名后10的国家:\n",df1.tail(10))

 然后用柱状图可视化绘制排名前十的分布情况

可见美国和中国是比较多的

plt.rcParams['font.size'] = 15
plt.rcParams['font.family'] = 'SimHei'
# # 拥有星巴克门店最多的国家是哪里?
# plt.figure(1,figsize=(8,6))
# count_starbucks =coffee.Country.value_counts()
# count_top10 = count_starbucks.head(10)
# print(count_top10)
# count_top10.plot(kind='bar',rot=0)
df1.head(10).plot(kind='bar',rot=0)
plt.title('星巴克门店数排名前10的国家')
plt.ylabel('Store Counts')
plt.xlabel('Countries')

还有排名后十的国家

 

# plt.figure(1,figsize=(8,6))
# count_starbucks =coffee.Country.value_counts()
# count_last10 = count_starbucks.tail(10)
# print(count_last10)
df1.tail(10).plot(kind='bar',rot=0)
plt.title('星巴克门店数排名后10的国家')
plt.ylabel('Store Counts')
plt.xlabel('Countries')

 接着显示拥有星巴克门店数量排名前十的城市

可见上海是最多的城市

star = starbucks.dropna(how='any',subset=['City'])
star.isnull().sum()
count_starbucks_city = star.City.value_counts()
print("全世界星巴克门店数量排名前10的城市:\n",count_starbucks_city.head(10))

 绘制星巴克门店数量前十的城市分布柱状图

plt.figure(1,figsize=(8,6))
count_starbucks_city =star.City.value_counts()
city_top10 = count_starbucks_city.head(10)
city_top10.plot(kind='bar',rot=30)
plt.title('拥有星巴克门店最多的10个城市')
plt.ylabel('Store Counts')
plt.xlabel('Cities')

 可以看到数据不是很规范,城市名称既有中文又有英文,而且上海被存储为ShangHai和Shanghai。 对于上海的问题,我们将拼音全部改为小写即可; 对于中文和拼音混用的问题,可以使用相应的python库(如库pinyin)将中文转换为拼音后作统计

按照星巴克门店在中国的分布情况,统计排名前十的城市

这里使用到了DataFrame.apply(func)方法,该方法将函数func应用到整个DataFrame上, 也可以通过指定axis参数来指定每一行或每一列的数据应用函数func。

接下来使用reset_index方法将上一步得到的数据封装到一个新的DataFrame中排序即可

 

import pinyin
#选择中国的数据
df = star[star["Country"]=="CN"]

df1 = df.copy()
#将城市名改为小写
df1["City"] = df1["City"].apply(lambda x:x.lower())
# df1.shape
# df2 = df1.copy()
#将汉字城市名改为小写拼音
df1["City"] = df1["City"].apply(lambda x:pinyin.get(x, format="strip", delimiter="")[0:-3]) #去掉“市”的拼音
#统计每个城市的星巴克数量
df1 = df1.groupby(["City"]).size().sort_values( ascending=False)
df1.head(10)

 绘制前十名柱状图

plt.figure(1,figsize=(8,6))
df1.head(10).plot(kind='bar',rot=30)
plt.title('中国拥有星巴克门店最多的10个城市')
plt.ylabel('Store Counts')
plt.xlabel('Cities')

 最后用饼状图显示星巴克门店的经营方式有哪几种

Company Owned:公司独资直营,这也是星巴克门店最多的经营方式

Licensed: 许可经营

Joint Venture: 合资经营,比如:国内江浙沪地区的星巴克最早就是由星巴克与统一集团联手经营

Franchise:授权经营,类似麦当劳的经营模式

 

 

plt.figure(1,figsize=(8,6))
ownership = star['Ownership Type'].value_counts()
plt.title('星巴克门店所有权类型')
ownership.plot(kind='pie')

创作不易 觉得有帮助请点赞关注收藏~~~

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
乘风的头像乘风管理团队
上一篇 2023年5月30日
下一篇 2023年5月30日

相关推荐