import numpy as np
import pandas as pd
data = pd.read_csv('directory.csv')# 读取数据,将文件转换为DataFrame格式
data.head()
data.describe()# 用于查看数值型数据的分布情况
data.info()# 用于查看各字段的数据类型,以及缺失情况
data['Brand'].unique()# 查看唯一品牌的类型
data = data[data['Brand'] == 'Starbucks']# 只获取品牌为Starbucks
data['Brand'].unique()
data.isnull().sum()# 各个字段缺失值的数量
data[data['City'].isnull()]# 查看缺失城市是哪些
# 用国家字段填充到城市字段上
def fill_na(x):
return x
data['City'] = data['City'].fillna(fill_na(data['State/Province']))
data[data['Country'] == 'EG']
#台湾被美国当成一个国家,把他重新赋值到中国来
data['Country'][data['Country'] == 'TW'] = 'CN'
country_count = data['Country'].value_counts()[0:10]
import matplotlib.pyplot as plt
#import matplotlib
#matplotlib.matplotlib_fname() # 将会获得matplotlib包所在文件夹
plt.rcParams['font.sans-serif'] = ['simhei']# 指定默认字体
plt.rcParams['axes.unicode_minus'] = False# 用来正常显示坐标轴上的负号(‘-’)
plt.title('全球星巴克数量前十的国家')
country_count.plot(kind = 'bar')
country_city_count = data['City'].value_counts()[0:10]
plt.title('全球星巴克数量前十的城市')
country_city_count.plot(kind = 'barh')
china_data = data[data['Country'] == 'CN']
china_data.head()
city_count = china_data['City'].value_counts()[0:10]
plt.title('中国星巴克数量前十的城市')
city_count.plot(kind = 'barh')