pandas练习(二)------ 数据过滤与排序
数据过滤与排序------探索2012欧洲杯数据
相关数据见(github)
步骤1 - 导入pandas库
import pandas as pd
步骤2 - 数据集
path2 = "./data/Euro2012.csv" # Euro2012.csv
步骤3 - 将数据集命名为euro12
euro12 = pd.read_csv(path2)
euro12.tail()
输出:
步骤4 选取 Goals
这一列
euro12.Goals # euro12['Goals']
输出:
步骤5 有多少球队参与了2012欧洲杯?
euro12.shape[0]
输出:
16
步骤6 该数据集中一共有多少列(columns)?
euro12.info()
输出:
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 16 entries, 0 to 15
Data columns (total 35 columns):
Team 16 non-null object
Goals 16 non-null int64
Shots on target 16 non-null int64
Shots off target 16 non-null int64
Shooting Accuracy 16 non-null object
% Goals-to-shots 16 non-null object
Total shots (inc. Blocked) 16 non-null int64
Hit Woodwork 16 non-null int64
Penalty goals 16 non-null int64
Penalties not scored 16 non-null int64
Headed goals 16 non-null int64
Passes 16 non-null int64
Passes completed 16 non-null int64
Passing Accuracy 16 non-null object
Touches 16 non-null int64
Crosses 16 non-null int64
Dribbles 16 non-null int64
Corners Taken 16 non-null int64
Tackles 16 non-null int64
Clearances 16 non-null int64
Interceptions 16 non-null int64
Clearances off line 15 non-null float64
Clean Sheets 16 non-null int64
Blocks 16 non-null int64
Goals conceded 16 non-null int64
Saves made 16 non-null int64
Saves-to-shots ratio 16 non-null object
Fouls Won 16 non-null int64
Fouls Conceded 16 non-null int64
Offsides 16 non-null int64
Yellow Cards 16 non-null int64
Red Cards 16 non-null int64
Subs on 16 non-null int64
Subs off 16 non-null int64
Players Used 16 non-null int64
dtypes: float64(1), int64(29), object(5)
memory usage: 4.5+ KB
步骤7 将数据集中的列Team, Yellow Cards和Red Cards单独存为一个名叫discipline的数据框
discipline = euro12[['Team', 'Yellow Cards', 'Red Cards']]
discipline
输出:
步骤8 对数据框discipline按照先Red Cards再Yellow Cards进行排序
discipline.sort_values(['Red Cards', 'Yellow Cards'], ascending = False)
输出:
步骤9 计算每个球队拿到的黄牌数的平均值
round(discipline['Yellow Cards'].mean())
输出:
7.0
步骤10 找到进球数Goals超过6的球队数据
euro12[euro12.Goals > 6]
输出:
步骤11 选取以字母G开头或以e结尾的球队数据
# euro12[euro12.Team.str.startswith('G')]
euro12[euro12.Team.str.endswith('e')] # 以字母e结束的球队
输出:
步骤12 选取前7列
euro12.iloc[: , 0:7]
输出:
步骤13 选取除了最后3列之外的全部列
euro12.iloc[: , :-3]
输出:
步骤14 找到英格兰(England)、意大利(Italy)和俄罗斯(Russia)的命中率(Shooting Accuracy)
euro12.loc[euro12.Team.isin(['England', 'Italy', 'Russia']), ['Team','Shooting Accuracy']]
输出:
参考链接:
1、http://pandas.pydata.org/pandas-docs/stable/cookbook.html#cookbook
2、https://www.analyticsvidhya.com/blog/2016/01/12-pandas-techniques-python-data-manipulation/
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端