Series索引和复合索引
DataFrame复合索引
使用matplotlib呈现出店铺总数排名前10的国家
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
# 以下两句是显示中文的方法
from pylab import *
mpl.rcParams['font.sans-serif'] = ['SimHei'] #有效的方法
file_path='C:/Users/ming/Desktop/DataAnalysis-master/day05/code/starbucks_store_worldwide.csv'
df=pd.read_csv(file_path)
# 使用matplotlib呈现出店铺总数排名前10的国家
# 准备数据,默认情况下是升序的我们需要按照降序的方式排列,所以设置ascending=False
data1=df.groupby(by='Country')['Brand'].count().sort_values(ascending=False)[:10]
_x=data1.index
_y=data1.values
# 画图
plt.figure(figsize=(8,6),dpi=80)
plt.bar(range(len(_x)),_y)
plt.xticks(range(len(_x)),_x)
plt.xlabel('国家')
plt.ylabel("店铺数量")
plt.title("国家和店铺数量")
plt.show()
使用matplotlib呈现出每个中国每个城市的店铺数量
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
# 以下两句是显示中文的方法
from pylab import *
mpl.rcParams['font.sans-serif'] = ['SimHei'] #有效的方法
file_path='C:/Users/ming/Desktop/DataAnalysis-master/day05/code/starbucks_store_worldwide.csv'
# 读取文件
df=pd.read_csv(file_path)
# 选中中国
df=df[df['Country']=='CN']
# 读取字段
print(df.info())
# 使用matplotlib呈现出店铺总数排名前10的国家
# 准备数据,默认情况下是升序的我们需要按照降序的方式排列,所以设置ascending=False
data1=df.groupby(by='City')['Brand'].count().sort_values(ascending=False)[:25]
_x=data1.index
_y=data1.values
# 画图
plt.figure(figsize=(8,6),dpi=80)
# plt.bar(range(len(_x)),_y,width=0.3,color='orange')
plt.barh(range(len(_x)),_y,height=0.3,color='orange')
plt.yticks(range(len(_x)),_x)
plt.xlabel('店铺数量')
plt.ylabel("城市")
plt.title("中国城市和店铺数量")
plt.show()
from matplotlib import pyplot as plt
import numpy as np
import pandas as pd
# 以下两句是显示中文的方法
from pylab import *
mpl.rcParams['font.sans-serif'] = ['SimHei'] #有效的方法
file_path='C:/Users/ming/Desktop/DataAnalysis-master/day05/code/books.csv'
df=pd.read_csv(file_path)
print(df.info())
# 不同年份书的数量
# 去除nan数据
# data1=df[pd.notnull(df['original_publication_year'])]
# grouped=data1.groupby(by='original_publication_year').count()['title']
# 不同年份数的评分情况
data1=df[pd.notnull(df['original_publication_year'])]
grouped=data1['average_rating'].groupby(by=data1['original_publication_year']).mean()
# print(grouped)
_x=grouped.index
_y=grouped.values
plt.plot(range(len(_x)),_y)
plt.xticks(list(range(len(_x)))[::10],_x[::10].astype(int),rotation=45)
plt.show()
总结