pandas_练习

这篇博客展示了如何使用pandas和matplotlib进行数据分组与可视化。首先,通过grouby函数分析了全球星巴克店铺按国家的分布,重点展示了店铺数量排名前十的国家。接着,聚焦中国,统计并绘制了各省份/城市的店铺数量,揭示了中国内部分布情况。最后,分析了书籍的平均评分与出版年份的关系,通过折线图呈现不同年份书籍的平均评分趋势。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、grouby 的练习

grouby 将df进行分组,选择某一列形成Series,之后进行画图
x轴:range (Series.index)
y轴:Series(或Series.value)

"""
@desc: 店铺排名前十的绘图
"""
import pandas as pd
from matplotlib import pyplot as plt
#显示所有列
pd.set_option('display.max_columns', None)
#显示所有行
pd.set_option('display.max_rows', None)
df=pd.read_csv("starbucks_store_worldwide.csv")
grouped=df.groupby(by="Country")["Country"].count()
grouped=grouped[grouped>10]
grouped=grouped.sort_values(ascending=False)
plt.figure(figsize=(20,8),dpi=80)
plt.bar(range(len(grouped)),grouped)
plt.xticks(range(len(grouped)),grouped.index)
plt.show()

在这里插入图片描述

@file: 21_中国每个城市店铺的数量.py 
@time: 2021/08/21 
@desc: 
"""
import pandas as pd
from matplotlib import pyplot as plt
#显示所有列
pd.set_option('display.max_columns', None)
#显示所有行
pd.set_option('display.max_rows', None)
df=pd.read_csv("starbucks_store_worldwide.csv")
df_cn=df[df["Country"]=="CN"]
grouped=df_cn.groupby(by="State/Province")["State/Province"].count()
grouped=grouped.sort_values(ascending=False)
print(grouped)
plt.figure(figsize=(20,8),dpi=80)
plt.bar(range(len(grouped)),grouped)
#
plt.xticks(range(len(grouped)),grouped.index)#其中中国各个城市名字以数字代替
plt.show()

在这里插入图片描述
在这里插入图片描述

二、不同年份书的评分情况

import pandas as pd
from matplotlib import pyplot as plt
#显示所有列
pd.set_option('display.max_columns', None)
#显示所有行
pd.set_option('display.max_rows', None)
df=pd.read_csv("books.csv")
df=df[pd.notnull(df["original_publication_year"])]#选择original_publication_year非NAN的值
grouped=df["average_rating"].groupby(by=df["original_publication_year"]).mean()
print(grouped)
plt.figure(figsize=(20,8),dpi=80)
plt.plot(range(len(grouped)),grouped)
plt.xticks(list(range(len(grouped)))[::10],list(grouped.index)[::10])
plt.show()


在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值