pandas分组聚合

现有一组关于全球星巴克店铺的统计数据,想知道排行最多的星巴克店铺数量的10国家。

  • 读取数据如下:
import pandas as pd

# 设置显示所有列
pd.set_option("display.max_columns", None)
# 读取csv文件
df = pd.read_csv("starbucks_store_worldwide.csv")
df.head(5)

在这里插入图片描述

  • 查看数据的大致情况,可以看到一共有2万多条数据,但有些数据是缺失的
# 查看表格整体情况
df.info()

在这里插入图片描述

  • 根据country进行分类聚合
# 分类聚合 计算每个国家星巴克店铺数量
countries_num = df.groupby(by="Country").count()["Brand"]
countries_num = countries_num.sort_values(ascending=False)[:10]
countries_num

在这里插入图片描述

  • 得到结果后画图,可以看到,作为美国本土的一个品牌,星巴克的门店再美国是最多的,而中国其次~
from matplotlib import pyplot as plt
import matplotlib 

# 设置中文显示

font = {
    'family' : 'simhei',
    'weight' : 'bold',
    'size'   : 16
}

matplotlib.rc("font", **font)

x = range(len(countries_num))
x_label = countries_num.index
y = countries_num.values

# 设置图形大小
plt.figure(figsize=(12,8), dpi=60)
plt.bar(x, y, color="g")

plt.xticks(x, x_label, fontsize=18)

for i,j in zip(x,y):
    plt.text(i-0.2,j+100, j, color="grey")
    
plt.title("星巴克店铺最多的前十个国家", fontsize=20)
plt.show()

在这里插入图片描述

现在,第二个问题,想知道中国每个省份星巴克的数量的情况,那么应该怎么办?

  • 先获取country为CN的数据
cn_df = df[df["Country"] == "CN"]
cn_df.head()

在这里插入图片描述

  • 根据省区的编号进行分类聚合
city_shop_num = cn_df.groupby(by="State/Province").count()["Brand"].sort_values(ascending=False)
city_shop_num = pd.DataFrame(city_shop_num.values,index=city_shop_num.index.astype("int"), columns=["num"])
city_shop_num

在这里插入图片描述

  • 然后我在网上找到了编号对应的省区,就是有两个编号对应的省区百度不到
country_codes = """
	北京市(京):11
	
	天津市(津):12
	
	上海市(沪):31
	
	重庆市(渝):50
	
	河北省(冀):13
	
	河南省(豫):41
	
	云南省(云):53
	
	辽宁省(辽):21
	
	黑龙江省(黑):23
	
	湖南省(湘):43
	
	安徽省(皖):34
	
	山东省(鲁):37
	
	新疆维吾尔(新):65
	
	江苏省(苏):32
	
	浙江省(浙):33
	
	江西省(赣):36
	
	湖北省(鄂):42
	
	广西壮族(桂):45
	
	甘肃省(甘):62
	
	山西省(晋):14
	
	内蒙古(蒙):15
	
	陕西省(陕):61
	
	吉林省(吉):22
	
	福建省(闽):35
	
	贵州省(贵):52
	
	广东省(粤):44
	
	青海省(青):63
	
	西藏(藏): 54
	
	四川省(川):51
	
	宁夏回族(宁):64
	
	海南省(琼):46
"""
country_codes = country_codes.split("\n\n")
country_codes = {c.split(":")[0]: int(c.split(":")[1]) for c in country_codes}
country_codes

在这里插入图片描述

  • 将编号对应的省区转换成DataFrame
counties = country_codes.keys()
codes = country_codes.values()
country_codes = pd.DataFrame(counties,index=codes)
country_codes = pd.DataFrame(country_codes[0].str.slice(-2,-1))
country_codes

在这里插入图片描述

  • 再将数据根据索引,即对应省区编号,进行拼接
city_shop_num = city_shop_num.join(country_codes)
city_shop_num

在这里插入图片描述

  • 处理为nan的数据
city_shop_num.dropna(inplace=True)
  • 画图,最后结果可以看到,星巴克店铺大部分分布在上海、江苏、浙江、北京、广东这些比较富饶的地区,嘿嘿,没钱还真消费不起
x = range(len(city_shop_num))
x_label = city_shop_num[0]
y = city_shop_num["num"]

plt.figure(figsize=(12,8),dpi=90)
plt.bar(x, y, color="g")

plt.xticks(x, x_label)

for i,j in zip(x,y):
    plt.text(i-0.4,j+4,j, fontsize=12)
    

plt.title("中国各省星巴克门店数量")
plt.show()

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值