In this notebook, we will look at different ways we can select columns from a master dataframe. This can be useful when creating a more relevant dataframe to undertake your analysis on.

By the end of this notebook you will be armed with multiple ways to select columns for your dataframe. These include:

  • Select columns by name
  • Select columns by datatype
  • Select columns by index
  • Select columns by condition statements

Getting ready

Let's start by importing pandas into our notebook

import pandas as pd

Next we will create our master dataframe by importing our dataset from a csv file.

df = pd.read_csv('data/players_22.csv', delimiter = ',')
C:\Users\pleas\AppData\Local\Temp\ipykernel_24464\110054706.py:1: DtypeWarning: Columns (25,108) have mixed types. Specify dtype option on import or set low_memory=False.
  df = pd.read_csv('data/players_22.csv', delimiter = ',')

Let's have a sneak peak at our dataframe

df.head()
sofifa_id player_url short_name long_name player_positions overall potential value_eur wage_eur age ... lcb cb rcb rb gk player_face_url club_logo_url club_flag_url nation_logo_url nation_flag_url
0 158023 https://sofifa.com/player/158023/lionel-messi/... L. Messi Lionel Andrés Messi Cuccittini RW, ST, CF 93 93 78000000.0 320000.0 34 ... 50+3 50+3 50+3 61+3 19+3 https://cdn.sofifa.net/players/158/023/22_120.png https://cdn.sofifa.net/teams/73/60.png https://cdn.sofifa.net/flags/fr.png https://cdn.sofifa.net/teams/1369/60.png https://cdn.sofifa.net/flags/ar.png
1 188545 https://sofifa.com/player/188545/robert-lewand... R. Lewandowski Robert Lewandowski ST 92 92 119500000.0 270000.0 32 ... 60+3 60+3 60+3 61+3 19+3 https://cdn.sofifa.net/players/188/545/22_120.png https://cdn.sofifa.net/teams/21/60.png https://cdn.sofifa.net/flags/de.png https://cdn.sofifa.net/teams/1353/60.png https://cdn.sofifa.net/flags/pl.png
2 20801 https://sofifa.com/player/20801/c-ronaldo-dos-... Cristiano Ronaldo Cristiano Ronaldo dos Santos Aveiro ST, LW 91 91 45000000.0 270000.0 36 ... 53+3 53+3 53+3 60+3 20+3 https://cdn.sofifa.net/players/020/801/22_120.png https://cdn.sofifa.net/teams/11/60.png https://cdn.sofifa.net/flags/gb-eng.png https://cdn.sofifa.net/teams/1354/60.png https://cdn.sofifa.net/flags/pt.png
3 190871 https://sofifa.com/player/190871/neymar-da-sil... Neymar Jr Neymar da Silva Santos Júnior LW, CAM 91 91 129000000.0 270000.0 29 ... 50+3 50+3 50+3 62+3 20+3 https://cdn.sofifa.net/players/190/871/22_120.png https://cdn.sofifa.net/teams/73/60.png https://cdn.sofifa.net/flags/fr.png NaN https://cdn.sofifa.net/flags/br.png
4 192985 https://sofifa.com/player/192985/kevin-de-bruy... K. De Bruyne Kevin De Bruyne CM, CAM 91 91 125500000.0 350000.0 30 ... 69+3 69+3 69+3 75+3 21+3 https://cdn.sofifa.net/players/192/985/22_120.png https://cdn.sofifa.net/teams/10/60.png https://cdn.sofifa.net/flags/gb-eng.png https://cdn.sofifa.net/teams/1325/60.png https://cdn.sofifa.net/flags/be.png

5 rows × 110 columns

At first glance, there seems to be a bunch of columns missing (truncated) from the head() output. Let's see how many columns we should have by running df.shape

df.shape
(19239, 110)

df.shape tells us how many rows and how may columns are in our dataframe. We are definitely not seeing all 110 columns in the head() output. That's ok, we can set it to show all columns by using pd.set_option().

pd.set_option('display.max_columns', None)

Let's see if it worked...

df.head()
sofifa_id player_url short_name long_name player_positions overall potential value_eur wage_eur age dob height_cm weight_kg club_team_id club_name league_name league_level club_position club_jersey_number club_loaned_from club_joined club_contract_valid_until nationality_id nationality_name nation_team_id nation_position nation_jersey_number preferred_foot weak_foot skill_moves international_reputation work_rate body_type real_face release_clause_eur player_tags player_traits pace shooting passing dribbling defending physic attacking_crossing attacking_finishing attacking_heading_accuracy attacking_short_passing attacking_volleys skill_dribbling skill_curve skill_fk_accuracy skill_long_passing skill_ball_control movement_acceleration movement_sprint_speed movement_agility movement_reactions movement_balance power_shot_power power_jumping power_stamina power_strength power_long_shots mentality_aggression mentality_interceptions mentality_positioning mentality_vision mentality_penalties mentality_composure defending_marking_awareness defending_standing_tackle defending_sliding_tackle goalkeeping_diving goalkeeping_handling goalkeeping_kicking goalkeeping_positioning goalkeeping_reflexes goalkeeping_speed ls st rs lw lf cf rf rw lam cam ram lm lcm cm rcm rm lwb ldm cdm rdm rwb lb lcb cb rcb rb gk player_face_url club_logo_url club_flag_url nation_logo_url nation_flag_url
0 158023 https://sofifa.com/player/158023/lionel-messi/... L. Messi Lionel Andrés Messi Cuccittini RW, ST, CF 93 93 78000000.0 320000.0 34 1987-06-24 170 72 73.0 Paris Saint-Germain French Ligue 1 1.0 RW 30.0 NaN 2021-08-10 2023.0 52 Argentina 1369.0 RW 10.0 Left 4 4 5 Medium/Low Unique Yes 144300000.0 #Dribbler, #Distance Shooter, #FK Specialist, ... Finesse Shot, Long Shot Taker (AI), Playmaker ... 85.0 92.0 91.0 95.0 34.0 65.0 85 95 70 91 88 96 93 94 91 96 91 80 91 94 95 86 68 72 69 94 44 40 93 95 75 96 20 35 24 6 11 15 14 8 NaN 89+3 89+3 89+3 92 93 93 93 92 93 93 93 91+2 87+3 87+3 87+3 91+2 66+3 64+3 64+3 64+3 66+3 61+3 50+3 50+3 50+3 61+3 19+3 https://cdn.sofifa.net/players/158/023/22_120.png https://cdn.sofifa.net/teams/73/60.png https://cdn.sofifa.net/flags/fr.png https://cdn.sofifa.net/teams/1369/60.png https://cdn.sofifa.net/flags/ar.png
1 188545 https://sofifa.com/player/188545/robert-lewand... R. Lewandowski Robert Lewandowski ST 92 92 119500000.0 270000.0 32 1988-08-21 185 81 21.0 FC Bayern München German 1. Bundesliga 1.0 ST 9.0 NaN 2014-07-01 2023.0 37 Poland 1353.0 RS 9.0 Right 4 4 5 High/Medium Unique Yes 197200000.0 #Aerial Threat, #Distance Shooter, #Clinical F... Solid Player, Finesse Shot, Outside Foot Shot,... 78.0 92.0 79.0 86.0 44.0 82.0 71 95 90 85 89 85 79 85 70 88 77 79 77 93 82 90 85 76 86 87 81 49 95 81 90 88 35 42 19 15 6 12 8 10 NaN 90+2 90+2 90+2 85 88 88 88 85 86+3 86+3 86+3 84+3 80+3 80+3 80+3 84+3 64+3 66+3 66+3 66+3 64+3 61+3 60+3 60+3 60+3 61+3 19+3 https://cdn.sofifa.net/players/188/545/22_120.png https://cdn.sofifa.net/teams/21/60.png https://cdn.sofifa.net/flags/de.png https://cdn.sofifa.net/teams/1353/60.png https://cdn.sofifa.net/flags/pl.png
2 20801 https://sofifa.com/player/20801/c-ronaldo-dos-... Cristiano Ronaldo Cristiano Ronaldo dos Santos Aveiro ST, LW 91 91 45000000.0 270000.0 36 1985-02-05 187 83 11.0 Manchester United English Premier League 1.0 ST 7.0 NaN 2021-08-27 2023.0 38 Portugal 1354.0 ST 7.0 Right 4 5 5 High/Low Unique Yes 83300000.0 #Aerial Threat, #Dribbler, #Distance Shooter, ... Power Free-Kick, Flair, Long Shot Taker (AI), ... 87.0 94.0 80.0 88.0 34.0 75.0 87 95 90 80 86 88 81 84 77 88 85 88 86 94 74 94 95 77 77 93 63 29 95 76 88 95 24 32 24 7 11 15 14 11 NaN 90+1 90+1 90+1 88 89 89 89 88 86+3 86+3 86+3 86+3 78+3 78+3 78+3 86+3 63+3 59+3 59+3 59+3 63+3 60+3 53+3 53+3 53+3 60+3 20+3 https://cdn.sofifa.net/players/020/801/22_120.png https://cdn.sofifa.net/teams/11/60.png https://cdn.sofifa.net/flags/gb-eng.png https://cdn.sofifa.net/teams/1354/60.png https://cdn.sofifa.net/flags/pt.png
3 190871 https://sofifa.com/player/190871/neymar-da-sil... Neymar Jr Neymar da Silva Santos Júnior LW, CAM 91 91 129000000.0 270000.0 29 1992-02-05 175 68 73.0 Paris Saint-Germain French Ligue 1 1.0 LW 10.0 NaN 2017-08-03 2025.0 54 Brazil NaN NaN NaN Right 5 5 5 High/Medium Unique Yes 238700000.0 #Speedster, #Dribbler, #Playmaker, #FK Special... Injury Prone, Flair, Speed Dribbler (AI), Play... 91.0 83.0 86.0 94.0 37.0 63.0 85 83 63 86 86 95 88 87 81 95 93 89 96 89 84 80 64 81 53 81 63 37 86 90 93 93 35 32 29 9 9 15 15 11 NaN 83+3 83+3 83+3 90 88 88 88 90 89+2 89+2 89+2 89+2 82+3 82+3 82+3 89+2 67+3 63+3 63+3 63+3 67+3 62+3 50+3 50+3 50+3 62+3 20+3 https://cdn.sofifa.net/players/190/871/22_120.png https://cdn.sofifa.net/teams/73/60.png https://cdn.sofifa.net/flags/fr.png NaN https://cdn.sofifa.net/flags/br.png
4 192985 https://sofifa.com/player/192985/kevin-de-bruy... K. De Bruyne Kevin De Bruyne CM, CAM 91 91 125500000.0 350000.0 30 1991-06-28 181 70 10.0 Manchester City English Premier League 1.0 RCM 17.0 NaN 2015-08-30 2025.0 7 Belgium 1325.0 RCM 7.0 Right 5 4 4 High/High Unique Yes 232200000.0 #Dribbler, #Playmaker, #Engine, #Distance Shoo... Injury Prone, Leadership, Early Crosser, Long ... 76.0 86.0 93.0 88.0 64.0 78.0 94 82 55 94 82 88 85 83 93 91 76 76 79 91 78 91 63 89 74 91 76 66 88 94 83 89 68 65 53 15 13 5 10 13 NaN 83+3 83+3 83+3 88 87 87 87 88 89+2 89+2 89+2 89+2 89+2 89+2 89+2 89+2 79+3 80+3 80+3 80+3 79+3 75+3 69+3 69+3 69+3 75+3 21+3 https://cdn.sofifa.net/players/192/985/22_120.png https://cdn.sofifa.net/teams/10/60.png https://cdn.sofifa.net/flags/gb-eng.png https://cdn.sofifa.net/teams/1325/60.png https://cdn.sofifa.net/flags/be.png

That looks more like 110 columns, however it is hard to read all the column names. Let's use df.info(), it will show us all the column names, their index, and also their datatype. This will come in handy later.

df.info(verbose=True)
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 19239 entries, 0 to 19238
Data columns (total 110 columns):
 #    Column                       Dtype  
---   ------                       -----  
 0    sofifa_id                    int64  
 1    player_url                   object 
 2    short_name                   object 
 3    long_name                    object 
 4    player_positions             object 
 5    overall                      int64  
 6    potential                    int64  
 7    value_eur                    float64
 8    wage_eur                     float64
 9    age                          int64  
 10   dob                          object 
 11   height_cm                    int64  
 12   weight_kg                    int64  
 13   club_team_id                 float64
 14   club_name                    object 
 15   league_name                  object 
 16   league_level                 float64
 17   club_position                object 
 18   club_jersey_number           float64
 19   club_loaned_from             object 
 20   club_joined                  object 
 21   club_contract_valid_until    float64
 22   nationality_id               int64  
 23   nationality_name             object 
 24   nation_team_id               float64
 25   nation_position              object 
 26   nation_jersey_number         float64
 27   preferred_foot               object 
 28   weak_foot                    int64  
 29   skill_moves                  int64  
 30   international_reputation     int64  
 31   work_rate                    object 
 32   body_type                    object 
 33   real_face                    object 
 34   release_clause_eur           float64
 35   player_tags                  object 
 36   player_traits                object 
 37   pace                         float64
 38   shooting                     float64
 39   passing                      float64
 40   dribbling                    float64
 41   defending                    float64
 42   physic                       float64
 43   attacking_crossing           int64  
 44   attacking_finishing          int64  
 45   attacking_heading_accuracy   int64  
 46   attacking_short_passing      int64  
 47   attacking_volleys            int64  
 48   skill_dribbling              int64  
 49   skill_curve                  int64  
 50   skill_fk_accuracy            int64  
 51   skill_long_passing           int64  
 52   skill_ball_control           int64  
 53   movement_acceleration        int64  
 54   movement_sprint_speed        int64  
 55   movement_agility             int64  
 56   movement_reactions           int64  
 57   movement_balance             int64  
 58   power_shot_power             int64  
 59   power_jumping                int64  
 60   power_stamina                int64  
 61   power_strength               int64  
 62   power_long_shots             int64  
 63   mentality_aggression         int64  
 64   mentality_interceptions      int64  
 65   mentality_positioning        int64  
 66   mentality_vision             int64  
 67   mentality_penalties          int64  
 68   mentality_composure          int64  
 69   defending_marking_awareness  int64  
 70   defending_standing_tackle    int64  
 71   defending_sliding_tackle     int64  
 72   goalkeeping_diving           int64  
 73   goalkeeping_handling         int64  
 74   goalkeeping_kicking          int64  
 75   goalkeeping_positioning      int64  
 76   goalkeeping_reflexes         int64  
 77   goalkeeping_speed            float64
 78   ls                           object 
 79   st                           object 
 80   rs                           object 
 81   lw                           object 
 82   lf                           object 
 83   cf                           object 
 84   rf                           object 
 85   rw                           object 
 86   lam                          object 
 87   cam                          object 
 88   ram                          object 
 89   lm                           object 
 90   lcm                          object 
 91   cm                           object 
 92   rcm                          object 
 93   rm                           object 
 94   lwb                          object 
 95   ldm                          object 
 96   cdm                          object 
 97   rdm                          object 
 98   rwb                          object 
 99   lb                           object 
 100  lcb                          object 
 101  cb                           object 
 102  rcb                          object 
 103  rb                           object 
 104  gk                           object 
 105  player_face_url              object 
 106  club_logo_url                object 
 107  club_flag_url                object 
 108  nation_logo_url              object 
 109  nation_flag_url              object 
dtypes: float64(16), int64(44), object(50)
memory usage: 16.1+ MB

What is a Pandas DataFrame made of?

A DataFrame is made up of both rows and columns. Each column has an index and a name. Looking at the df.info output above, we can see the first column has an index of 0 and the name is "sofifa_id".

Selecting Columns by Name

Using our list of column names above, we can easily return a single column.

name1_df = df.short_name

name1_df
0                  L. Messi
1            R. Lewandowski
2         Cristiano Ronaldo
3                 Neymar Jr
4              K. De Bruyne
                ...        
19234             Song Defu
19235             C. Porter
19236              N. Logue
19237             L. Rudden
19238    E. Lalchhanchhuaha
Name: short_name, Length: 19239, dtype: object
name2_df = df['short_name']

name2_df
0                  L. Messi
1            R. Lewandowski
2         Cristiano Ronaldo
3                 Neymar Jr
4              K. De Bruyne
                ...        
19234             Song Defu
19235             C. Porter
19236              N. Logue
19237             L. Rudden
19238    E. Lalchhanchhuaha
Name: short_name, Length: 19239, dtype: object

These two methods are simple enough, however we were hoping to create new DataFrames. Let's use type() to check our new objects type.

type(name1_df)
pandas.core.series.Series
type(name2_df)
pandas.core.series.Series

We can see that both methods have actually created a Pandas Series pandas.core.series.Series. In order to create a single column DataFrame, we need to use double brackets.

name3_df = df[['short_name']]

name3_df
short_name
0 L. Messi
1 R. Lewandowski
2 Cristiano Ronaldo
3 Neymar Jr
4 K. De Bruyne
... ...
19234 Song Defu
19235 C. Porter
19236 N. Logue
19237 L. Rudden
19238 E. Lalchhanchhuaha

19239 rows × 1 columns

Now when we run type() we should get a DataFrame.

type(name3_df)
pandas.core.frame.DataFrame

We can use this same syntax to select multiple columns

name_club_age_df = df[['short_name', 'club_name', 'age']]

display(name_club_age_df)

print("Type: ",type(name_club_age_df))
short_name club_name age
0 L. Messi Paris Saint-Germain 34
1 R. Lewandowski FC Bayern München 32
2 Cristiano Ronaldo Manchester United 36
3 Neymar Jr Paris Saint-Germain 29
4 K. De Bruyne Manchester City 30
... ... ... ...
19234 Song Defu Wuhan FC 22
19235 C. Porter Derry City 19
19236 N. Logue Finn Harps 21
19237 L. Rudden Finn Harps 19
19238 E. Lalchhanchhuaha NorthEast United FC 19

19239 rows × 3 columns

Type:  <class 'pandas.core.frame.DataFrame'>

A benefit of stipulating each column by name is the ability to re-order columns.

name_age_club_df = df[['short_name', 'age', 'club_name']]

display(name_age_club_df)

print("Type: ",type(name_age_club_df))
short_name age club_name
0 L. Messi 34 Paris Saint-Germain
1 R. Lewandowski 32 FC Bayern München
2 Cristiano Ronaldo 36 Manchester United
3 Neymar Jr 29 Paris Saint-Germain
4 K. De Bruyne 30 Manchester City
... ... ... ...
19234 Song Defu 22 Wuhan FC
19235 C. Porter 19 Derry City
19236 N. Logue 21 Finn Harps
19237 L. Rudden 19 Finn Harps
19238 E. Lalchhanchhuaha 19 NorthEast United FC

19239 rows × 3 columns

Type:  <class 'pandas.core.frame.DataFrame'>

Selecting Columns by Data Type

You may find yourself in a position where you wish to create a new DataFrame using only columns of a particular data type such as integers or floats. Let's run df.info again to see the data types of all our columns.

df.info(verbose=True)
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 19239 entries, 0 to 19238
Data columns (total 110 columns):
 #    Column                       Dtype  
---   ------                       -----  
 0    sofifa_id                    int64  
 1    player_url                   object 
 2    short_name                   object 
 3    long_name                    object 
 4    player_positions             object 
 5    overall                      int64  
 6    potential                    int64  
 7    value_eur                    float64
 8    wage_eur                     float64
 9    age                          int64  
 10   dob                          object 
 11   height_cm                    int64  
 12   weight_kg                    int64  
 13   club_team_id                 float64
 14   club_name                    object 
 15   league_name                  object 
 16   league_level                 float64
 17   club_position                object 
 18   club_jersey_number           float64
 19   club_loaned_from             object 
 20   club_joined                  object 
 21   club_contract_valid_until    float64
 22   nationality_id               int64  
 23   nationality_name             object 
 24   nation_team_id               float64
 25   nation_position              object 
 26   nation_jersey_number         float64
 27   preferred_foot               object 
 28   weak_foot                    int64  
 29   skill_moves                  int64  
 30   international_reputation     int64  
 31   work_rate                    object 
 32   body_type                    object 
 33   real_face                    object 
 34   release_clause_eur           float64
 35   player_tags                  object 
 36   player_traits                object 
 37   pace                         float64
 38   shooting                     float64
 39   passing                      float64
 40   dribbling                    float64
 41   defending                    float64
 42   physic                       float64
 43   attacking_crossing           int64  
 44   attacking_finishing          int64  
 45   attacking_heading_accuracy   int64  
 46   attacking_short_passing      int64  
 47   attacking_volleys            int64  
 48   skill_dribbling              int64  
 49   skill_curve                  int64  
 50   skill_fk_accuracy            int64  
 51   skill_long_passing           int64  
 52   skill_ball_control           int64  
 53   movement_acceleration        int64  
 54   movement_sprint_speed        int64  
 55   movement_agility             int64  
 56   movement_reactions           int64  
 57   movement_balance             int64  
 58   power_shot_power             int64  
 59   power_jumping                int64  
 60   power_stamina                int64  
 61   power_strength               int64  
 62   power_long_shots             int64  
 63   mentality_aggression         int64  
 64   mentality_interceptions      int64  
 65   mentality_positioning        int64  
 66   mentality_vision             int64  
 67   mentality_penalties          int64  
 68   mentality_composure          int64  
 69   defending_marking_awareness  int64  
 70   defending_standing_tackle    int64  
 71   defending_sliding_tackle     int64  
 72   goalkeeping_diving           int64  
 73   goalkeeping_handling         int64  
 74   goalkeeping_kicking          int64  
 75   goalkeeping_positioning      int64  
 76   goalkeeping_reflexes         int64  
 77   goalkeeping_speed            float64
 78   ls                           object 
 79   st                           object 
 80   rs                           object 
 81   lw                           object 
 82   lf                           object 
 83   cf                           object 
 84   rf                           object 
 85   rw                           object 
 86   lam                          object 
 87   cam                          object 
 88   ram                          object 
 89   lm                           object 
 90   lcm                          object 
 91   cm                           object 
 92   rcm                          object 
 93   rm                           object 
 94   lwb                          object 
 95   ldm                          object 
 96   cdm                          object 
 97   rdm                          object 
 98   rwb                          object 
 99   lb                           object 
 100  lcb                          object 
 101  cb                           object 
 102  rcb                          object 
 103  rb                           object 
 104  gk                           object 
 105  player_face_url              object 
 106  club_logo_url                object 
 107  club_flag_url                object 
 108  nation_logo_url              object 
 109  nation_flag_url              object 
dtypes: float64(16), int64(44), object(50)
memory usage: 16.1+ MB

The output tells us we have 16 columns that are float64, 44 columns that are int64 and 50 columns that are object type. We can use select_dtypes to choose columns based on their data types.

float64_df = df.select_dtypes('float64')

display(float64_df)

print("Type: ", type(float64_df))
value_eur wage_eur club_team_id league_level club_jersey_number club_contract_valid_until nation_team_id nation_jersey_number release_clause_eur pace shooting passing dribbling defending physic goalkeeping_speed
0 78000000.0 320000.0 73.0 1.0 30.0 2023.0 1369.0 10.0 144300000.0 85.0 92.0 91.0 95.0 34.0 65.0 NaN
1 119500000.0 270000.0 21.0 1.0 9.0 2023.0 1353.0 9.0 197200000.0 78.0 92.0 79.0 86.0 44.0 82.0 NaN
2 45000000.0 270000.0 11.0 1.0 7.0 2023.0 1354.0 7.0 83300000.0 87.0 94.0 80.0 88.0 34.0 75.0 NaN
3 129000000.0 270000.0 73.0 1.0 10.0 2025.0 NaN NaN 238700000.0 91.0 83.0 86.0 94.0 37.0 63.0 NaN
4 125500000.0 350000.0 10.0 1.0 17.0 2025.0 1325.0 7.0 232200000.0 76.0 86.0 93.0 88.0 64.0 78.0 NaN
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
19234 70000.0 1000.0 112541.0 1.0 36.0 2021.0 NaN NaN 114000.0 58.0 35.0 46.0 48.0 42.0 49.0 NaN
19235 110000.0 500.0 445.0 1.0 27.0 2021.0 NaN NaN 193000.0 59.0 39.0 50.0 46.0 41.0 51.0 NaN
19236 100000.0 500.0 111131.0 1.0 31.0 2021.0 NaN NaN 175000.0 60.0 37.0 45.0 49.0 41.0 52.0 NaN
19237 110000.0 500.0 111131.0 1.0 12.0 2021.0 NaN NaN 239000.0 68.0 46.0 36.0 48.0 15.0 42.0 NaN
19238 110000.0 500.0 113040.0 1.0 13.0 2025.0 NaN NaN 217000.0 68.0 38.0 45.0 48.0 36.0 48.0 NaN

19239 rows × 16 columns

Type:  <class 'pandas.core.frame.DataFrame'>
object_df = df.select_dtypes('object')

display(object_df)

print("Type: ", type(object_df))
player_url short_name long_name player_positions dob club_name league_name club_position club_loaned_from club_joined nationality_name nation_position preferred_foot work_rate body_type real_face player_tags player_traits ls st rs lw lf cf rf rw lam cam ram lm lcm cm rcm rm lwb ldm cdm rdm rwb lb lcb cb rcb rb gk player_face_url club_logo_url club_flag_url nation_logo_url nation_flag_url
0 https://sofifa.com/player/158023/lionel-messi/... L. Messi Lionel Andrés Messi Cuccittini RW, ST, CF 1987-06-24 Paris Saint-Germain French Ligue 1 RW NaN 2021-08-10 Argentina RW Left Medium/Low Unique Yes #Dribbler, #Distance Shooter, #FK Specialist, ... Finesse Shot, Long Shot Taker (AI), Playmaker ... 89+3 89+3 89+3 92 93 93 93 92 93 93 93 91+2 87+3 87+3 87+3 91+2 66+3 64+3 64+3 64+3 66+3 61+3 50+3 50+3 50+3 61+3 19+3 https://cdn.sofifa.net/players/158/023/22_120.png https://cdn.sofifa.net/teams/73/60.png https://cdn.sofifa.net/flags/fr.png https://cdn.sofifa.net/teams/1369/60.png https://cdn.sofifa.net/flags/ar.png
1 https://sofifa.com/player/188545/robert-lewand... R. Lewandowski Robert Lewandowski ST 1988-08-21 FC Bayern München German 1. Bundesliga ST NaN 2014-07-01 Poland RS Right High/Medium Unique Yes #Aerial Threat, #Distance Shooter, #Clinical F... Solid Player, Finesse Shot, Outside Foot Shot,... 90+2 90+2 90+2 85 88 88 88 85 86+3 86+3 86+3 84+3 80+3 80+3 80+3 84+3 64+3 66+3 66+3 66+3 64+3 61+3 60+3 60+3 60+3 61+3 19+3 https://cdn.sofifa.net/players/188/545/22_120.png https://cdn.sofifa.net/teams/21/60.png https://cdn.sofifa.net/flags/de.png https://cdn.sofifa.net/teams/1353/60.png https://cdn.sofifa.net/flags/pl.png
2 https://sofifa.com/player/20801/c-ronaldo-dos-... Cristiano Ronaldo Cristiano Ronaldo dos Santos Aveiro ST, LW 1985-02-05 Manchester United English Premier League ST NaN 2021-08-27 Portugal ST Right High/Low Unique Yes #Aerial Threat, #Dribbler, #Distance Shooter, ... Power Free-Kick, Flair, Long Shot Taker (AI), ... 90+1 90+1 90+1 88 89 89 89 88 86+3 86+3 86+3 86+3 78+3 78+3 78+3 86+3 63+3 59+3 59+3 59+3 63+3 60+3 53+3 53+3 53+3 60+3 20+3 https://cdn.sofifa.net/players/020/801/22_120.png https://cdn.sofifa.net/teams/11/60.png https://cdn.sofifa.net/flags/gb-eng.png https://cdn.sofifa.net/teams/1354/60.png https://cdn.sofifa.net/flags/pt.png
3 https://sofifa.com/player/190871/neymar-da-sil... Neymar Jr Neymar da Silva Santos Júnior LW, CAM 1992-02-05 Paris Saint-Germain French Ligue 1 LW NaN 2017-08-03 Brazil NaN Right High/Medium Unique Yes #Speedster, #Dribbler, #Playmaker, #FK Special... Injury Prone, Flair, Speed Dribbler (AI), Play... 83+3 83+3 83+3 90 88 88 88 90 89+2 89+2 89+2 89+2 82+3 82+3 82+3 89+2 67+3 63+3 63+3 63+3 67+3 62+3 50+3 50+3 50+3 62+3 20+3 https://cdn.sofifa.net/players/190/871/22_120.png https://cdn.sofifa.net/teams/73/60.png https://cdn.sofifa.net/flags/fr.png NaN https://cdn.sofifa.net/flags/br.png
4 https://sofifa.com/player/192985/kevin-de-bruy... K. De Bruyne Kevin De Bruyne CM, CAM 1991-06-28 Manchester City English Premier League RCM NaN 2015-08-30 Belgium RCM Right High/High Unique Yes #Dribbler, #Playmaker, #Engine, #Distance Shoo... Injury Prone, Leadership, Early Crosser, Long ... 83+3 83+3 83+3 88 87 87 87 88 89+2 89+2 89+2 89+2 89+2 89+2 89+2 89+2 79+3 80+3 80+3 80+3 79+3 75+3 69+3 69+3 69+3 75+3 21+3 https://cdn.sofifa.net/players/192/985/22_120.png https://cdn.sofifa.net/teams/10/60.png https://cdn.sofifa.net/flags/gb-eng.png https://cdn.sofifa.net/teams/1325/60.png https://cdn.sofifa.net/flags/be.png
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
19234 https://sofifa.com/player/261962/defu-song/220002 Song Defu 宋德福 CDM 1999-01-02 Wuhan FC Chinese Super League SUB NaN 2021-04-01 China PR NaN Right Medium/Medium Lean (170-185) No NaN NaN 44+2 44+2 44+2 46 45 45 45 46 46+2 46+2 46+2 47+2 46+2 46+2 46+2 47+2 48+2 46+2 46+2 46+2 48+2 48+2 46+2 46+2 46+2 48+2 15+2 https://cdn.sofifa.net/players/261/962/22_120.png https://cdn.sofifa.net/teams/112541/60.png https://cdn.sofifa.net/flags/cn.png NaN https://cdn.sofifa.net/flags/cn.png
19235 https://sofifa.com/player/262040/caoimhin-port... C. Porter Caoimhin Porter CM 2002-03-18 Derry City Rep. Ireland Airtricity League RES NaN 2021-02-22 Republic of Ireland NaN Right Medium/Medium Lean (170-185) No NaN NaN 45+2 45+2 45+2 47 46 46 46 47 47+2 47+2 47+2 49+2 47+2 47+2 47+2 49+2 49+2 46+2 46+2 46+2 49+2 48+2 44+2 44+2 44+2 48+2 14+2 https://cdn.sofifa.net/players/262/040/22_120.png https://cdn.sofifa.net/teams/445/60.png https://cdn.sofifa.net/flags/ie.png NaN https://cdn.sofifa.net/flags/ie.png
19236 https://sofifa.com/player/262760/nathan-logue/... N. Logue Nathan Logue-Cunningham CM 2000-02-07 Finn Harps Rep. Ireland Airtricity League RES NaN 2021-06-18 Republic of Ireland NaN Right Medium/Medium Lean (170-185) No NaN NaN 45+2 45+2 45+2 46 46 46 46 46 47+2 47+2 47+2 48+2 47+2 47+2 47+2 48+2 47+2 46+2 46+2 46+2 47+2 47+2 45+2 45+2 45+2 47+2 12+2 https://cdn.sofifa.net/players/262/760/22_120.png https://cdn.sofifa.net/teams/111131/60.png https://cdn.sofifa.net/flags/ie.png NaN https://cdn.sofifa.net/flags/ie.png
19237 https://sofifa.com/player/262820/luke-rudden/2... L. Rudden Luke Rudden ST 2002-04-23 Finn Harps Rep. Ireland Airtricity League RES NaN 2021-06-18 Republic of Ireland NaN Right Medium/Medium Lean (170-185) No NaN NaN 47+2 47+2 47+2 47 47 47 47 47 45+2 45+2 45+2 45+2 39+2 39+2 39+2 45+2 33+2 29+2 29+2 29+2 33+2 32+2 26+2 26+2 26+2 32+2 15+2 https://cdn.sofifa.net/players/262/820/22_120.png https://cdn.sofifa.net/teams/111131/60.png https://cdn.sofifa.net/flags/ie.png NaN https://cdn.sofifa.net/flags/ie.png
19238 https://sofifa.com/player/264540/emanuel-lalch... E. Lalchhanchhuaha Emanuel Lalchhanchhuaha CAM 2002-02-06 NorthEast United FC Indian Super League SUB NaN 2021-08-31 India NaN Right Medium/Medium Lean (170-) No NaN NaN 45+2 45+2 45+2 47 47 47 47 47 47+2 47+2 47+2 48+2 46+2 46+2 46+2 48+2 45+2 44+2 44+2 44+2 45+2 45+2 41+2 41+2 41+2 45+2 16+2 https://cdn.sofifa.net/players/264/540/22_120.png https://cdn.sofifa.net/teams/113040/60.png https://cdn.sofifa.net/flags/in.png NaN https://cdn.sofifa.net/flags/in.png

19239 rows × 50 columns

Type:  <class 'pandas.core.frame.DataFrame'>
int64_df = df.select_dtypes('int64')

display(int64_df)

print("Type: ", type(int64_df))
sofifa_id overall potential age height_cm weight_kg nationality_id weak_foot skill_moves international_reputation attacking_crossing attacking_finishing attacking_heading_accuracy attacking_short_passing attacking_volleys skill_dribbling skill_curve skill_fk_accuracy skill_long_passing skill_ball_control movement_acceleration movement_sprint_speed movement_agility movement_reactions movement_balance power_shot_power power_jumping power_stamina power_strength power_long_shots mentality_aggression mentality_interceptions mentality_positioning mentality_vision mentality_penalties mentality_composure defending_marking_awareness defending_standing_tackle defending_sliding_tackle goalkeeping_diving goalkeeping_handling goalkeeping_kicking goalkeeping_positioning goalkeeping_reflexes
0 158023 93 93 34 170 72 52 4 4 5 85 95 70 91 88 96 93 94 91 96 91 80 91 94 95 86 68 72 69 94 44 40 93 95 75 96 20 35 24 6 11 15 14 8
1 188545 92 92 32 185 81 37 4 4 5 71 95 90 85 89 85 79 85 70 88 77 79 77 93 82 90 85 76 86 87 81 49 95 81 90 88 35 42 19 15 6 12 8 10
2 20801 91 91 36 187 83 38 4 5 5 87 95 90 80 86 88 81 84 77 88 85 88 86 94 74 94 95 77 77 93 63 29 95 76 88 95 24 32 24 7 11 15 14 11
3 190871 91 91 29 175 68 54 5 5 5 85 83 63 86 86 95 88 87 81 95 93 89 96 89 84 80 64 81 53 81 63 37 86 90 93 93 35 32 29 9 9 15 15 11
4 192985 91 91 30 181 70 7 5 4 4 94 82 55 94 82 88 85 83 93 91 76 76 79 91 78 91 63 89 74 91 76 66 88 94 83 89 68 65 53 15 13 5 10 13
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
19234 261962 47 52 22 180 64 155 3 2 1 46 32 48 50 30 45 33 38 48 49 56 60 55 53 70 46 62 51 46 30 52 42 38 43 42 37 38 43 48 6 10 5 15 13
19235 262040 47 59 19 175 70 25 3 2 1 54 33 46 51 32 41 53 31 50 42 60 58 64 49 69 49 47 63 47 38 49 39 51 49 44 47 37 44 47 11 12 6 8 10
19236 262760 47 55 21 178 72 25 3 2 1 39 32 43 49 37 47 37 37 49 49 60 60 58 46 59 50 57 56 50 34 51 38 45 46 39 36 38 44 48 8 6 7 10 6
19237 262820 47 60 19 173 66 25 3 2 1 29 49 40 38 34 42 36 34 33 45 69 67 72 48 73 48 50 50 40 41 34 14 47 40 49 47 10 14 11 7 10 7 14 15
19238 264540 47 60 19 167 61 159 3 2 1 39 32 46 50 37 48 38 32 49 38 70 67 65 54 85 50 62 57 40 36 55 42 47 46 35 36 33 31 37 8 13 13 9 14

19239 rows × 44 columns

Type:  <class 'pandas.core.frame.DataFrame'>

Selecting Columns Using loc

loc is a great way to select row and columns. The syntax can look confusing at first, but let's break it down.

df.loc[a:b, x:y]

The first argument a:b refers to the rows you wish to select. As we are focussing on selecting columns in this notebook, we will use : which returns all rows.

The second argument x:y refers to the columns you want to select. There are three main ways we can use this. We can :

  1. Select a single column df.loc[:,['short_name']]
  2. Select multiple columns df.loc[:,['short_name', 'overall', 'value_eur']]
  3. Select a slice of columns df.loc[:,['short_name':'age']

The slice method will select the two columns and any other that are inbetween.

name_df = df.loc[:, ['short_name']]

display(name_df)

print("Type: ", type(name_df))
short_name
0 L. Messi
1 R. Lewandowski
2 Cristiano Ronaldo
3 Neymar Jr
4 K. De Bruyne
... ...
19234 Song Defu
19235 C. Porter
19236 N. Logue
19237 L. Rudden
19238 E. Lalchhanchhuaha

19239 rows × 1 columns

Type:  <class 'pandas.core.frame.DataFrame'>
name_overall_value_df = df.loc[:,['short_name', 'overall', 'value_eur']]

display(name_overall_value_df)

print("Type: ", type(name_overall_value_df))
short_name overall value_eur
0 L. Messi 93 78000000.0
1 R. Lewandowski 92 119500000.0
2 Cristiano Ronaldo 91 45000000.0
3 Neymar Jr 91 129000000.0
4 K. De Bruyne 91 125500000.0
... ... ... ...
19234 Song Defu 47 70000.0
19235 C. Porter 47 110000.0
19236 N. Logue 47 100000.0
19237 L. Rudden 47 110000.0
19238 E. Lalchhanchhuaha 47 110000.0

19239 rows × 3 columns

Type:  <class 'pandas.core.frame.DataFrame'>
name_age_slice_df = df.loc[:,'short_name':'age']

display(name_age_slice_df)

print("Type: ", type(name_age_slice_df))
short_name long_name player_positions overall potential value_eur wage_eur age
0 L. Messi Lionel Andrés Messi Cuccittini RW, ST, CF 93 93 78000000.0 320000.0 34
1 R. Lewandowski Robert Lewandowski ST 92 92 119500000.0 270000.0 32
2 Cristiano Ronaldo Cristiano Ronaldo dos Santos Aveiro ST, LW 91 91 45000000.0 270000.0 36
3 Neymar Jr Neymar da Silva Santos Júnior LW, CAM 91 91 129000000.0 270000.0 29
4 K. De Bruyne Kevin De Bruyne CM, CAM 91 91 125500000.0 350000.0 30
... ... ... ... ... ... ... ... ...
19234 Song Defu 宋德福 CDM 47 52 70000.0 1000.0 22
19235 C. Porter Caoimhin Porter CM 47 59 110000.0 500.0 19
19236 N. Logue Nathan Logue-Cunningham CM 47 55 100000.0 500.0 21
19237 L. Rudden Luke Rudden ST 47 60 110000.0 500.0 19
19238 E. Lalchhanchhuaha Emanuel Lalchhanchhuaha CAM 47 60 110000.0 500.0 19

19239 rows × 8 columns

Type:  <class 'pandas.core.frame.DataFrame'>

Selecting Columns using iloc

iloc is very similar to loc, however instead of using column names, we can use the columns index. The same three methods can be achieved using iloc

iloc_name_df = df.iloc[:,[2]]

display(iloc_name_df)

print("Type: ", type(iloc_name_df))
short_name
0 L. Messi
1 R. Lewandowski
2 Cristiano Ronaldo
3 Neymar Jr
4 K. De Bruyne
... ...
19234 Song Defu
19235 C. Porter
19236 N. Logue
19237 L. Rudden
19238 E. Lalchhanchhuaha

19239 rows × 1 columns

Type:  <class 'pandas.core.frame.DataFrame'>
iloc_name_overall_value_df = df.iloc[:,[2, 5, 7]]

display(iloc_name_overall_value_df)

print("Type: ", type(iloc_name_overall_value_df))
short_name overall value_eur
0 L. Messi 93 78000000.0
1 R. Lewandowski 92 119500000.0
2 Cristiano Ronaldo 91 45000000.0
3 Neymar Jr 91 129000000.0
4 K. De Bruyne 91 125500000.0
... ... ... ...
19234 Song Defu 47 70000.0
19235 C. Porter 47 110000.0
19236 N. Logue 47 100000.0
19237 L. Rudden 47 110000.0
19238 E. Lalchhanchhuaha 47 110000.0

19239 rows × 3 columns

Type:  <class 'pandas.core.frame.DataFrame'>
iloc_name_age_slice = df.iloc[:,2:9]

display(iloc_name_age_slice)

print("Type: ", type(iloc_name_age_slice))
short_name long_name player_positions overall potential value_eur wage_eur
0 L. Messi Lionel Andrés Messi Cuccittini RW, ST, CF 93 93 78000000.0 320000.0
1 R. Lewandowski Robert Lewandowski ST 92 92 119500000.0 270000.0
2 Cristiano Ronaldo Cristiano Ronaldo dos Santos Aveiro ST, LW 91 91 45000000.0 270000.0
3 Neymar Jr Neymar da Silva Santos Júnior LW, CAM 91 91 129000000.0 270000.0
4 K. De Bruyne Kevin De Bruyne CM, CAM 91 91 125500000.0 350000.0
... ... ... ... ... ... ... ...
19234 Song Defu 宋德福 CDM 47 52 70000.0 1000.0
19235 C. Porter Caoimhin Porter CM 47 59 110000.0 500.0
19236 N. Logue Nathan Logue-Cunningham CM 47 55 100000.0 500.0
19237 L. Rudden Luke Rudden ST 47 60 110000.0 500.0
19238 E. Lalchhanchhuaha Emanuel Lalchhanchhuaha CAM 47 60 110000.0 500.0

19239 rows × 7 columns

Type:  <class 'pandas.core.frame.DataFrame'>