Pandas exercises (ii)------data filtering and sorting

Source: Internet
Author: User

Data filtering and sorting------Explore 2012 Euro Cup data

Related data See (github)

Step 1-Import the Pandas library
import Pandas as PD
Step 2-Data set
" ./data/euro2012.csv "      # Euro2012.csv
Step 3-Name the dataset euro12
Euro12 = pd.read_csv (path2) euro12.tail ()

Output:

Total hit ... on
Team goals Shots on target Shots off target Shooting accuracy % goals-to-shotsShots (inc. Blocked)woodwork penalty goals Penalties not scored Saves made Saves-to-shots ratio fouls Won fouls conceded offsides Yellow Cards Red Cards SubsSubs 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% 34 43 4 6 0 7 7 16
13 Spain 12 42 33 55.9% 16% 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% 38 0 0 0 ... 13 76.5% 48 31 4 5 0 9 9 18

5 rowsx35 Columns

Step 4 Select GoalsThis column
euro12. Goals  #   euro12[' goals ')

Output:

Step 5 How many teams are involved in the 2012 Euro Cup?
EURO12.SHAPE[0]

Output:

16
Step 6 How many columns (columns) are there in the data set?
Euro12.info ()

Output:

<class ' Pandas.core.frame.DataFrame ' >rangeindex:16 entries, 0 to 15Data columns (total columns): Team Non-null objectgoals non-null int64shots on target non-null i              Nt64shots off target non-null int64shooting accuracy non-null object% goals-to-shots Objecttotal Shots (inc Blocked) non-null non-null int64hit woodwork non-null Int64pen Alty goals Non-null Int64penalties not scored 1 Non-null int64headed goals 6 int64passes non-null int64passes completed non-null non-null int64passing ACCU Racy non-null objecttouches non-null int64crosses                       ll Int64dribbles non-null Int64corners taken Non-null int64tackles Non-null inT64clearances non-null int64interceptions non-null int64clearances off line Non-null Float64clean Sheets non-null int64blocks, Non-null int64goal  S conceded non-null Int64saves made-non-null int64saves-to-shots ratio 16                      Non-null objectfouls Won non-null Int64fouls conceded-non-null int64offsides Non-null int64yellow Cards non-null int64red Cards                  L int64subs on Non-null int64subs off Non-null int64players used Non-null Int64dtypes:float64 (1), Int64, Object (5) Memory usage:4.5+ KB
Step 7 Save the columns in the dataset team, Yellow cards, and red cards as a single data frame named discipline
Discipline = euro12[['Team'Yellow Cards'Red Cards']]discipline

Output:

Step 8 to the data frame discipline according to the first red cards and then yellow cards to sort
Discipline.sort_values (['Red Cards'Yellow Cards'], ascending = False)

Output:

Step 9 Calculate the average of the number of yellow cards each team gets
Round (discipline['Yellow Cards'].mean ())

Output:

7.0
Step 10 Find team data for the number of goals goals over 6
Euro12[euro12. Goals > 6]

Output:

 

 

Team goals Shots on target Shot s 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% 100 0 1 0 ... 15 93.8% 102 83 19 11 0 17 17 18

2 rowsx35 Columns

Step 11 Select Team data that begins with the letter G or ends with E
# euro12[euro12. Team.str.startswith (' G ')]euro12[euro12. Team.str.endswith ('e')]  #  team ending with the letter e

Output:

td>65
  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 [ ] 37.9% 6.5% 1 0 0 ... 6 54.6% 6 5 0 one one
6 Greece 5 8 / 30.7% 19.2% 1 1 1 ... 65.1% 9 1 + / + 12
7 Ukraine 2 up 21.2% 6.0% 0 0 0 ... 76.5% 4 5 9 0 9 +

3 rowsx35 Columns

Step 12 Select the first 7 columns
euro12.iloc[:, 0:7]

Output:

Step 13 Select all columns except the last 3 columns
euro12.iloc[:,:-3]

Output:

Total Hit Clean
Team goals Shots on target Shots off target Shooting accuracy % goals-to-shotsShots (inc. Blocked)woodwork Penalty Goals penalties not scored ...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% 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% 20% 27 1 0 0 ... 1 10 5 10 66.7% 25 38 8 4 0
3 England 5 11 18 50% 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% 7.5% 110 2 0 0 ... 2 18 7 20 74.1% 101 89 16 16 0
8 Netherlands 2 12 36 25% 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% 34 43 4 6 0
13 Spain 12 42 33 55.9% 16% 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% 38 0 0 0 ... 0 4 4 13 76.5% 48 31 4 5 0

rowsx32 columns

Step 14 Find the hit ratio in England (England), Italy (Italy) and Russia (Russia) (Shooting accuracy)
Euro12.loc[euro12. Team.isin (['England'Italy'Russia  ']), ['Team','Shooting accuracy']

Output:

Pandas exercises (ii)------data filtering and sorting

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.