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()

输出:

  Team Goals Shots on target Shots off target Shooting Accuracy % Goals-to-shots Total shots (inc. Blocked) Hit Woodwork Penalty goals Penalties not scored ... Saves made Saves-to-shots ratio Fouls Won Fouls Conceded Offsides Yellow Cards Red Cards Subs on Subs off Players Used
11 Republic of Ireland 1 7 12 36.8% 5.2% 28 0 0 0 ... 17 65.4% 43 51 11 6 1 10 10 17
12 Russia 5 9 31 22.5% 12.5% 59 2 0 0 ... 10 77.0% 34 43 4 6 0 7 7 16
13 Spain 12 42 33 55.9% 16.0% 100 0 1 0 ... 15 93.8% 102 83 19 11 0 17 17 18
14 Sweden 5 17 19 47.2% 13.8% 39 3 0 0 ... 8 61.6% 35 51 7 7 0 9 9 18
15 Ukraine 2 7 26 21.2% 6.0% 38 0 0 0 ... 13 76.5% 48 31 4 5 0 9 9 18

5 rows × 35 columns

步骤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]

输出:

Team Goals Shots on target Shots off target Shooting Accuracy % Goals-to-shots Total shots (inc. Blocked) Hit Woodwork Penalty goals Penalties not scored ... Saves made Saves-to-shots ratio Fouls Won Fouls Conceded Offsides Yellow Cards Red Cards Subs on Subs off Players Used
5 Germany 10 32 32 47.8% 15.6% 80 2 1 0 ... 10 62.6% 63 49 12 4 0 15 15 17
13 Spain 12 42 33 55.9% 16.0% 100 0 1 0 ... 15 93.8% 102 83 19 11 0 17 17 18

2 rows × 35 columns

步骤11 选取以字母G开头或以e结尾的球队数据

# euro12[euro12.Team.str.startswith(‘G‘)]
euro12[euro12.Team.str.endswith(‘e‘)]  # 以字母e结束的球队

输出:

  Team Goals Shots on target Shots off target Shooting Accuracy % Goals-to-shots Total shots (inc. Blocked) Hit Woodwork Penalty goals Penalties not scored ... Saves made Saves-to-shots ratio Fouls Won Fouls Conceded Offsides Yellow Cards Red Cards Subs on Subs off Players Used
4 France 3 22 24 37.9% 6.5% 65 1 0 0 ... 6 54.6% 36 51 5 6 0 11 11 19
6 Greece 5 8 18 30.7% 19.2% 32 1 1 1 ... 13 65.1% 67 48 12 9 1 12 12 20
15 Ukraine 2 7 26 21.2% 6.0% 38 0 0 0 ... 13 76.5% 48 31 4 5 0 9 9 18

3 rows × 35 columns

步骤12 选取前7列

euro12.iloc[: , 0:7]

输出:

步骤13 选取除了最后3列之外的全部列

euro12.iloc[: , :-3]

输出:

  Team Goals Shots on target Shots off target Shooting Accuracy % Goals-to-shots Total shots (inc. Blocked) Hit Woodwork Penalty goals Penalties not scored ... Clean Sheets Blocks Goals conceded Saves made Saves-to-shots ratio Fouls Won Fouls Conceded Offsides Yellow Cards Red Cards
0 Croatia 4 13 12 51.9% 16.0% 32 0 0 0 ... 0 10 3 13 81.3% 41 62 2 9 0
1 Czech Republic 4 13 18 41.9% 12.9% 39 0 0 0 ... 1 10 6 9 60.1% 53 73 8 7 0
2 Denmark 4 10 10 50.0% 20.0% 27 1 0 0 ... 1 10 5 10 66.7% 25 38 8 4 0
3 England 5 11 18 50.0% 17.2% 40 0 0 0 ... 2 29 3 22 88.1% 43 45 6 5 0
4 France 3 22 24 37.9% 6.5% 65 1 0 0 ... 1 7 5 6 54.6% 36 51 5 6 0
5 Germany 10 32 32 47.8% 15.6% 80 2 1 0 ... 1 11 6 10 62.6% 63 49 12 4 0
6 Greece 5 8 18 30.7% 19.2% 32 1 1 1 ... 1 23 7 13 65.1% 67 48 12 9 1
7 Italy 6 34 45 43.0% 7.5% 110 2 0 0 ... 2 18 7 20 74.1% 101 89 16 16 0
8 Netherlands 2 12 36 25.0% 4.1% 60 2 0 0 ... 0 9 5 12 70.6% 35 30 3 5 0
9 Poland 2 15 23 39.4% 5.2% 48 0 0 0 ... 0 8 3 6 66.7% 48 56 3 7 1
10 Portugal 6 22 42 34.3% 9.3% 82 6 0 0 ... 2 11 4 10 71.5% 73 90 10 12 0
11 Republic of Ireland 1 7 12 36.8% 5.2% 28 0 0 0 ... 0 23 9 17 65.4% 43 51 11 6 1
12 Russia 5 9 31 22.5% 12.5% 59 2 0 0 ... 0 8 3 10 77.0% 34 43 4 6 0
13 Spain 12 42 33 55.9% 16.0% 100 0 1 0 ... 5 8 1 15 93.8% 102 83 19 11 0
14 Sweden 5 17 19 47.2% 13.8% 39 3 0 0 ... 1 12 5 8 61.6% 35 51 7 7 0
15 Ukraine 2 7 26 21.2% 6.0% 38 0 0 0 ... 0 4 4 13 76.5% 48 31 4 5 0

16 rows × 32 columns

步骤14 找到英格兰(England)、意大利(Italy)和俄罗斯(Russia)的命中率(Shooting Accuracy)

euro12.loc[euro12.Team.isin([‘England‘, ‘Italy‘, ‘Russia‘]), [‘Team‘,‘Shooting Accuracy‘]]

输出:

原文地址:https://www.cnblogs.com/xiaxuexiaoab/p/9176699.html

时间: 2024-11-06 07:18:31

pandas练习(二)------ 数据过滤与排序的相关文章

Oracle学习(二):过滤和排序

1.知识点:可以对照下面的录屏进行阅读 SQL> --字符串大小写敏感 SQL> --查询名叫KING的员工信息 SQL> select * 2 from emp 3 where ename = 'KING'; SQL> --日期格式敏感 SQL> --查询入职日期为17-11月-81的员工 SQL> select * 2 from emp 3 where hiredate='17-11月-81'; --正确例子 SQL> ed 已写入 file afiedt.b

pandas索引的数据查找、排序和去重小结

由于Pandas的索引比较复杂,常常在使用过程中容易搞混,所以整理一份关于索引的查找.排序.去重的总结文档. import pandas as pd import numpy as np #定义DataFrame dict={'a':[1,2,3],'b':[4,5,6],'c':[7,8,9]} df=pd.DataFrame(dict,index=['one','two','three']) df .dataframe tbody tr th:only-of-type { vertical-

Oracle 第2节 过滤和排序数据

LessonAim While retrieving data from the database, you may need to restrict the rows of data that are displayed or specify the order in which the rows are displayed. This lesson explains the SQL statements that youuse to perform these actions. ?在查询中过

【Django REST framework】DRF快速编写接口(查询所有数据、分页、搜索、过滤、排序、新增单条数据)

所需依赖包及版本号 [packages]django = {index = "https://pypi.douban.com/simple",version = "==2.2.10"}djangorestframework = "==3.11.0"markdown = "==3.2.1"pygments = "==2.5.2"django-filter = "==2.2.0"django

Rxjava2 Observable的数据过滤详解及实例(二)

目录 6. Filter 7. Frist 7.1 firstElement() 7.2 first(defaultItem) 7.3 firstOrError() 8. Single 8.1 singleElement() 8.2 single(defaultItem) 8.3 singleOrError() 9. ElementAt 9.1 elementAt(index) 9.2 elementAt(index, defaultItem) 9.3 elementAtOrError(inde

SQL语句汇总(二)——数据修改、数据查询

首先创建一张表如下,创建表的方法在上篇介绍过了,这里就不再赘述. 添加新数据: INSERT INTO <表名> (<列名列表>) VALUES (<值列表>)  如: INSERT INTO t_student (student_id,student_name,student_age,student_sex) VALUES (1,'大毛',18,'男'); 其中列名可以省略,省略之后要求插入的值必须与列一一对应: INSERT INTO t_student VALUE

drf框架 token签发与校验、过滤/筛选/排序/分页组件

一.token 1.签发token 1)源码: # 前提:给一个局部禁用了所有 认证与权限 的视图类发送用户信息得到token,其实就是登录接口 # 1)rest_framework_jwt.views.ObtainJSONWebToken 的 父类 JSONWebTokenAPIView 的 post 方法 # 接受有username.password的post请求 # 2)post方法将请求数据交给 rest_framework_jwt.serializer.JSONWebTokenSeri

Oracle学习(2):过滤和排序

Oracle的过滤与排序 where过滤语法 SQL> --查询10号部门的员工 SQL> select * 2  from emp 3  where deptno=10; EMPNO ENAME    JOB              MGR HIREDATE         SAL       COMM     DEPTNO ---------- -------- --------- ---------- -------------- ----- ---------- ----------

ExtJS 4.2 业务开发(二)数据展示和查询

本篇开始模拟一个船舶管理系统,提供查询.添加.修改船舶的功能,这里介绍其中的数据展示和查询功能. 目录 1. 数据展示 2. 数据查询 3. 在线演示 1. 数据展示 在这里我们将模拟一个船舶管理系统,并提供查询.添加.修改的功能. 大致的目录结构如下: ShipMgrTab.js :船舶业务的入口. controller 目录:存放船舶业务的逻辑控制文件. model 目录:存放船舶业务的model文件. store 目录 :存放船舶业务的store文件. view 目录 :存放船舶业务的组件