Select Pandas Dataframe Rows And Columns Using iloc loc and ix

In this post, I will talk about how to use Python library Pandas iloc, loc and ix functions to select rows and columns from csv and excel files

I will be using college.csv data which has details about university admissions.

Lets start with importing pandas library and read_csv to read the csv file

In [1]:
import pandas as pd
In [2]:
df = pd.read_csv('College.csv')
In [4]:
df.head(2)
Out[4]:
Unnamed: 0 Private Apps Accept Enroll Top10perc Top25perc F.Undergrad P.Undergrad Outstate Room.Board Books Personal PhD Terminal S.F.Ratio perc.alumni Expend Grad.Rate
0 Abilene Christian University Yes 1660 1232 721 23 52 2885 537 7440 3300 450 2200 70 78 18.1 12 7041 60
1 Adelphi University Yes 2186 1924 512 16 29 2683 1227 12280 6450 750 1500 29 30 12.2 16 10527 56

How To Use dataframe.loc To Select Rows

Lets check what df.loc actually used for, if you do df.loc?, you will find following documentation...

Access a group of rows and columns by label(s) or a boolean array.

.loc[] is primarily label based, but may also be used with a boolean array.

Lets try to select the columns by labels first. We will have to use double index [[]]

In [9]:
df.loc[['Apps']]

I got following error

KeyError: "None of [Index(['Apps'], dtype='object')] are in the [index]"

The reason for above error is that Apps is not an index. We will have to set the column names as index first. Lets see what is index set to right now.

In [14]:
df.index
Out[14]:
RangeIndex(start=0, stop=777, step=1)

The index is set from 0 to 777 which is actually row numbers. Of course we can use current index to select the rows. Lets try that.

In [15]:
df.loc[0]
Out[15]:
Unnamed: 0     Abilene Christian University
Private                                 Yes
Apps                                   1660
Accept                                 1232
Enroll                                  721
Top10perc                                23
Top25perc                                52
F.Undergrad                            2885
P.Undergrad                             537
Outstate                               7440
Room.Board                             3300
Books                                   450
Personal                               2200
PhD                                      70
Terminal                                 78
S.F.Ratio                              18.1
perc.alumni                              12
Expend                                 7041
Grad.Rate                                60
Name: 0, dtype: object

As We see we got the first row. Lets add column University Name as index and see what happens. We will have to rename the University Name which is Unname:0 in dataframe.

In [25]:
df.rename(columns={'Unnamed: 0':'univ'},inplace=True)
In [39]:
df.set_index('univ',inplace=True)
In [40]:
df.head(2)
Out[40]:
Private Apps Accept Enroll Top10perc Top25perc F.Undergrad P.Undergrad Outstate Room.Board Books Personal PhD Terminal S.F.Ratio perc.alumni Expend Grad.Rate
univ
Abilene Christian University Yes 1660 1232 721 23 52 2885 537 7440 3300 450 2200 70 78 18.1 12 7041 60
Adelphi University Yes 2186 1924 512 16 29 2683 1227 12280 6450 750 1500 29 30 12.2 16 10527 56

Lets Try to select the row by university name

In [44]:
df.loc['Abilene Christian University']
Out[44]:
Private         Yes
Apps           1660
Accept         1232
Enroll          721
Top10perc        23
Top25perc        52
F.Undergrad    2885
P.Undergrad     537
Outstate       7440
Room.Board     3300
Books           450
Personal       2200
PhD              70
Terminal         78
S.F.Ratio      18.1
perc.alumni      12
Expend         7041
Grad.Rate        60
Name: Abilene Christian University, dtype: object

How do you remove the indexes. Use reset_index()

In [47]:
df = df.reset_index()
In [48]:
df.head(1)
Out[48]:
univ Private Apps Accept Enroll Top10perc Top25perc F.Undergrad P.Undergrad Outstate Room.Board Books Personal PhD Terminal S.F.Ratio perc.alumni Expend Grad.Rate
0 Abilene Christian University Yes 1660 1232 721 23 52 2885 537 7440 3300 450 2200 70 78 18.1 12 7041 60

How To Use dataframe.loc To Select Columns

Lets set the university as index again. This time, we want to select a particular column data for a partiular row.

In [49]:
df.set_index('univ',inplace=True)
In [51]:
df.loc['Abilene Christian University',['Apps']]
Out[51]:
Apps    1660
Name: Abilene Christian University, dtype: object

Lets say we want to select Apps column for two rows. Check the double indexes [[]] for rows

In [52]:
df.loc[['Abilene Christian University','Adelphi University'],['Apps']]
Out[52]:
Apps
univ
Abilene Christian University 1660
Adelphi University 2186

Lets say we want to print all the rows for column 'Apps'. Look out for syntax : in the below command, it means all the rows

In [53]:
df.loc[:,'Apps']
Out[53]:
univ
Abilene Christian University       1660
Adelphi University                 2186
Adrian College                     1428
Agnes Scott College                 417
Alaska Pacific University           193
                                  ...  
Worcester State College            2197
Xavier University                  1959
Xavier University of Louisiana     2097
Yale University                   10705
York College of Pennsylvania       2989
Name: Apps, Length: 777, dtype: int64

How To Use dataframe.iloc To Select

We can give the rows a range, lets say we want to select first 2 rows and just print the column Apps

In [71]:
df.iloc[:2,:]
Out[71]:
Private Apps Accept Enroll Top10perc Top25perc F.Undergrad P.Undergrad Outstate Room.Board Books Personal PhD Terminal S.F.Ratio perc.alumni Expend Grad.Rate
univ
Abilene Christian University Yes 1660 1232 721 23 52 2885 537 7440 3300 450 2200 70 78 18.1 12 7041 60
Adelphi University Yes 2186 1924 512 16 29 2683 1227 12280 6450 750 1500 29 30 12.2 16 10527 56

We can give it different index numbers. Print rows 1 ,4 and 5

In [75]:
df.iloc[[1,4,5],:]
Out[75]:
Private Apps Accept Enroll Top10perc Top25perc F.Undergrad P.Undergrad Outstate Room.Board Books Personal PhD Terminal S.F.Ratio perc.alumni Expend Grad.Rate
univ
Adelphi University Yes 2186 1924 512 16 29 2683 1227 12280 6450 750 1500 29 30 12.2 16 10527 56
Alaska Pacific University Yes 193 146 55 16 44 249 869 7560 4120 800 1500 76 72 11.9 2 10922 15
Albertson College Yes 587 479 158 38 62 678 41 13500 3335 500 675 67 73 9.4 11 9727 55

Lets try the above command by the univ index names but for that you will have to use the loc command

In [77]:
df.loc[['Adelphi University','Alaska Pacific University','Albertson College'],:]
Out[77]:
Private Apps Accept Enroll Top10perc Top25perc F.Undergrad P.Undergrad Outstate Room.Board Books Personal PhD Terminal S.F.Ratio perc.alumni Expend Grad.Rate
univ
Adelphi University Yes 2186 1924 512 16 29 2683 1227 12280 6450 750 1500 29 30 12.2 16 10527 56
Alaska Pacific University Yes 193 146 55 16 44 249 869 7560 4120 800 1500 76 72 11.9 2 10922 15
Albertson College Yes 587 479 158 38 62 678 41 13500 3335 500 675 67 73 9.4 11 9727 55

How To Use .ix in Pandas

Pandas .ix has been deprecated in latest version. But I will just over it really quick. .ix is a hybrid of both loc and iloc. Meaning we can use both the syntaxes of loc and iloc and Pandas will figure out the right operation. Lets try with an example

In [79]:
import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)
df.ix[['Adelphi University','Alaska Pacific University','Albertson College'],:]
Out[79]:
Private Apps Accept Enroll Top10perc Top25perc F.Undergrad P.Undergrad Outstate Room.Board Books Personal PhD Terminal S.F.Ratio perc.alumni Expend Grad.Rate
univ
Adelphi University Yes 2186 1924 512 16 29 2683 1227 12280 6450 750 1500 29 30 12.2 16 10527 56
Alaska Pacific University Yes 193 146 55 16 44 249 869 7560 4120 800 1500 76 72 11.9 2 10922 15
Albertson College Yes 587 479 158 38 62 678 41 13500 3335 500 675 67 73 9.4 11 9727 55

Note: I used "import warnings" module just to supress the future warnings. Otherwise you would see big warning message about ix that .ix has been deprecated.

Regarding .ix, if you noticed as used the same command that i used for loc (In [77]) and I got the same result. Lets try one with iloc.

In [80]:
df.ix[:2,:]
Out[80]:
Private Apps Accept Enroll Top10perc Top25perc F.Undergrad P.Undergrad Outstate Room.Board Books Personal PhD Terminal S.F.Ratio perc.alumni Expend Grad.Rate
univ
Abilene Christian University Yes 1660 1232 721 23 52 2885 537 7440 3300 450 2200 70 78 18.1 12 7041 60
Adelphi University Yes 2186 1924 512 16 29 2683 1227 12280 6450 750 1500 29 30 12.2 16 10527 56

There You go, we got the same result that we got with the iloc command used above.