需要源碼和數(shù)據(jù)集請(qǐng)點(diǎn)贊關(guān)注收藏后評(píng)論區(qū)留言私信~~~
下面對(duì)一組關(guān)于全球星巴克門店的統(tǒng)計(jì)數(shù)據(jù),分析了在不同國家和地區(qū)以及中國不同城市的星巴克門店的數(shù)量
1:導(dǎo)入模塊
import pandas as pd
import numpy as np
from pandas import Series,DataFrame
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei'] #用來正常顯示中文標(biāo)簽
plt.rcParams['axes.unicode_minus']=False #用來正常顯示負(fù)號(hào)
%matplotlib inline
2:獲取數(shù)據(jù) 并打印前五行
starbucks = pd.read_csv("data//directory.csv")
starbucks.head()
?3:數(shù)據(jù)分析及可視化
首先查看星巴克旗下有哪些品牌,如果我們只關(guān)心星巴克咖啡門店,則只需要獲取星巴克中Brand的數(shù)據(jù)集,并查看全世界一共有多少家星巴克門門店
# 星巴克旗下有哪些品牌?
print("星巴克旗下品牌有:\n",starbucks.Brand.value_counts())
# 把重心放在星巴克的咖啡門店上面,所以只查看Brand是Starbucks的數(shù)據(jù)集內(nèi)容。
coffee = starbucks[starbucks.Brand=='Starbucks']
# 全世界一共有多少家星巴克門店?
print("-------------------------")
print(coffee.shape)
?然后查看全世界一共有多少個(gè)國家和地區(qū)開設(shè)了星巴克門店,顯示門店數(shù)量排名前十和后十的國家和地區(qū)
?
df = starbucks.groupby(["Country"]).size()
print("全世界一共多少個(gè)國家開設(shè)了星巴克門店:",df.size)
df1 = df.sort_values( ascending=False)
print("排名前10的國家:\n",df1.head(10))
# 星巴克門店數(shù)排名后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('星巴克門店數(shù)排名前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('星巴克門店數(shù)排名后10的國家')
plt.ylabel('Store Counts')
plt.xlabel('Countries')
?接著顯示擁有星巴克門店數(shù)量排名前十的城市
可見上海是最多的城市
star = starbucks.dropna(how='any',subset=['City'])
star.isnull().sum()
count_starbucks_city = star.City.value_counts()
print("全世界星巴克門店數(shù)量排名前10的城市:\n",count_starbucks_city.head(10))
?繪制星巴克門店數(shù)量前十的城市分布柱狀圖
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個(gè)城市')
plt.ylabel('Store Counts')
plt.xlabel('Cities')
?可以看到數(shù)據(jù)不是很規(guī)范,城市名稱既有中文又有英文,而且上海被存儲(chǔ)為ShangHai和Shanghai。 對(duì)于上海的問題,我們將拼音全部改為小寫即可; 對(duì)于中文和拼音混用的問題,可以使用相應(yīng)的python庫(如庫pinyin)將中文轉(zhuǎn)換為拼音后作統(tǒng)計(jì)
按照星巴克門店在中國的分布情況,統(tǒng)計(jì)排名前十的城市
這里使用到了DataFrame.apply(func)方法,該方法將函數(shù)func應(yīng)用到整個(gè)DataFrame上, 也可以通過指定axis參數(shù)來指定每一行或每一列的數(shù)據(jù)應(yīng)用函數(shù)func。
接下來使用reset_index方法將上一步得到的數(shù)據(jù)封裝到一個(gè)新的DataFrame中排序即可
?文章來源:http://www.zghlxwxcb.cn/news/detail-462918.html
import pinyin
#選擇中國的數(shù)據(jù)
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]) #去掉“市”的拼音
#統(tǒng)計(jì)每個(gè)城市的星巴克數(shù)量
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個(gè)城市')
plt.ylabel('Store Counts')
plt.xlabel('Cities')
?最后用餅狀圖顯示星巴克門店的經(jīng)營方式有哪幾種
Company Owned:公司獨(dú)資直營,這也是星巴克門店最多的經(jīng)營方式
Licensed: 許可經(jīng)營
Joint Venture: 合資經(jīng)營,比如:國內(nèi)江浙滬地區(qū)的星巴克最早就是由星巴克與統(tǒng)一集團(tuán)聯(lián)手經(jīng)營
Franchise:授權(quán)經(jīng)營,類似麥當(dāng)勞的經(jīng)營模式
?
?
plt.figure(1,figsize=(8,6))
ownership = star['Ownership Type'].value_counts()
plt.title('星巴克門店所有權(quán)類型')
ownership.plot(kind='pie')
創(chuàng)作不易 覺得有幫助請(qǐng)點(diǎn)贊關(guān)注收藏~~~文章來源地址http://www.zghlxwxcb.cn/news/detail-462918.html
到了這里,關(guān)于Python統(tǒng)計(jì)全球星巴克門店的數(shù)據(jù)及在不同國家和地區(qū)門店數(shù)量可視化(超詳細(xì) 附源碼)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!