pandans_数据过滤与排序

2023-01-15  本文已影响0人  敬子v

数据源:链接: https://pan.baidu.com/s/1EFqJFXf70t2Rubkh6D19aw 提取码: syqg
探索2012欧洲杯数据
数据源示例

步骤1 - 导入必要的库

import pandas as pd

步骤2 - 从以下地址导入数据集

path1='pandas_exercise\exercise_data\Euro2012_stats.csv'

步骤3 - 将数据集命名为euro12

euro12=pd.read_csv(path1)
print(euro12.head())

步骤4 只选取 Goals 这一列

print(euro12.Goals)

步骤5 有多少球队参与了2012欧洲杯?

print(euro12.shape[0])

步骤6 该数据集中一共有多少列(columns)?

print(euro12.info())

步骤7 将数据集中的列Team, Yellow Cards和Red Cards单独存为一个名叫discipline的数据框

discipline=euro12[['Team','Yellow Cards','Red Cards']]
print(discipline)

步骤8 对数据框discipline按照先Red Cards再Yellow Cards进行排序

discipline.sort_values(['Red Cards','Yellow Cards'],ascending=False,inplace=True)
print(discipline)

步骤9 计算每个球队拿到的黄牌数的平均值

print(discipline['Yellow Cards'].mean())

步骤10 找到进球数Goals超过6的球队数据

print(euro12[euro12.Goals>6])

步骤11 选取以字母G开头的球队数据 .str.startswith

print(euro12[euro12.Team.str.startswith('G')])

步骤12 选取前7列 取值 .iloc[:,0:7]

print(euro12.iloc[:,0:7])

步骤13 选取除了最后3列之外的全部列 .iloc[:,:-3]

print(euro12.iloc[:,:-3])

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

loc(条件,所取得列)
print(euro12.loc[euro12.Team.isin(['England','Italy','Russia']),['Team','Shooting Accuracy']])

# 步骤3
          Team  Goals  Shots on target  ...  Subs on Subs off Players Used
0         Croatia      4               13  ...        9        9           16
1  Czech Republic      4               13  ...       11       11           19
2         Denmark      4               10  ...        7        7           15
3         England      5               11  ...       11       11           16
4          France      3               22  ...       11       11           19
[5 rows x 35 columns]
# 步骤5
0      4
1      4
2      4
3      5
4      3
5     10
6      5
7      6
8      2
9      2
10     6
11     1
12     5
13    12
14     5
15     2
Name: Goals, dtype: int64
# 步骤6
16
# 步骤7
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 16 entries, 0 to 15
Data columns (total 35 columns):
 #   Column                      Non-Null Count  Dtype  
---  ------                      --------------  -----  
 0   Team                        16 non-null     object 
 1   Goals                       16 non-null     int64  
 2   Shots on target             16 non-null     int64  
 3   Shots off target            16 non-null     int64  
 4   Shooting Accuracy           16 non-null     object 
 5   % Goals-to-shots            16 non-null     object 
 6   Total shots (inc. Blocked)  16 non-null     int64  
 7   Hit Woodwork                16 non-null     int64  
 8   Penalty goals               16 non-null     int64  
 9   Penalties not scored        16 non-null     int64  
 10  Headed goals                16 non-null     int64  
 11  Passes                      16 non-null     int64  
 12  Passes completed            16 non-null     int64  
 13  Passing Accuracy            16 non-null     object 
 14  Touches                     16 non-null     int64  
 15  Crosses                     16 non-null     int64  
 16  Dribbles                    16 non-null     int64  
 17  Corners Taken               16 non-null     int64  
 18  Tackles                     16 non-null     int64  
 19  Clearances                  16 non-null     int64  
 20  Interceptions               16 non-null     int64  
 21  Clearances off line         15 non-null     float64
 22  Clean Sheets                16 non-null     int64  
 23  Blocks                      16 non-null     int64  
 24  Goals conceded              16 non-null     int64  
 25  Saves made                  16 non-null     int64  
 26  Saves-to-shots ratio        16 non-null     object 
 27  Fouls Won                   16 non-null     int64  
 28  Fouls Conceded              16 non-null     int64  
 29  Offsides                    16 non-null     int64  
 30  Yellow Cards                16 non-null     int64  
 31  Red Cards                   16 non-null     int64  
 32  Subs on                     16 non-null     int64  
 33  Subs off                    16 non-null     int64  
 34  Players Used                16 non-null     int64  
dtypes: float64(1), int64(29), object(5)
memory usage: 4.5+ KB
None
# 步骤8
                  Team  Yellow Cards  Red Cards
6                Greece             9          1
9                Poland             7          1
11  Republic of Ireland             6          1
7                 Italy            16          0
10             Portugal            12          0
13                Spain            11          0
0               Croatia             9          0
1        Czech Republic             7          0
14               Sweden             7          0
4                France             6          0
12               Russia             6          0
3               England             5          0
8           Netherlands             5          0
15              Ukraine             5          0
2               Denmark             4          0
5               Germany             4          0

# 步骤9
7.4375
# 步骤10
       Team  Goals  Shots on target  ...  Subs on Subs off Players Used
5   Germany     10               32  ...       15       15           17
13    Spain     12               42  ...       17       17           18
[2 rows x 35 columns]
# 步骤11
      Team  Goals  Shots on target  ...  Subs on Subs off Players Used
5  Germany     10               32  ...       15       15           17
6   Greece      5                8  ...       12       12           20
[2 rows x 35 columns]
# 步骤12
                   Team  Goals  ...  % Goals-to-shots  Total shots (inc. Blocked)
0               Croatia      4  ...             16.0%                          32
1        Czech Republic      4  ...             12.9%                          39
2               Denmark      4  ...             20.0%                          27
3               England      5  ...             17.2%                          40
4                France      3  ...              6.5%                          65
5               Germany     10  ...             15.6%                          80
6                Greece      5  ...             19.2%                          32
7                 Italy      6  ...              7.5%                         110
8           Netherlands      2  ...              4.1%                          60
9                Poland      2  ...              5.2%                          48
10             Portugal      6  ...              9.3%                          82
11  Republic of Ireland      1  ...              5.2%                          28
12               Russia      5  ...             12.5%                          59
13                Spain     12  ...             16.0%                         100
14               Sweden      5  ...             13.8%                          39
15              Ukraine      2  ...              6.0%                          38
[16 rows x 7 columns]
# 步骤13
                   Team  Goals  ...  Yellow Cards  Red Cards
0               Croatia      4  ...             9          0
1        Czech Republic      4  ...             7          0
2               Denmark      4  ...             4          0
3               England      5  ...             5          0
4                France      3  ...             6          0
5               Germany     10  ...             4          0
6                Greece      5  ...             9          1
7                 Italy      6  ...            16          0
8           Netherlands      2  ...             5          0
9                Poland      2  ...             7          1
10             Portugal      6  ...            12          0
11  Republic of Ireland      1  ...             6          1
12               Russia      5  ...             6          0
13                Spain     12  ...            11          0
14               Sweden      5  ...             7          0
15              Ukraine      2  ...             5          0
[16 rows x 32 columns]
# 步骤14
       Team Shooting Accuracy
3   England             50.0%
7     Italy             43.0%
12   Russia             22.5%

上一篇下一篇

猜你喜欢

热点阅读