Indexing with iloc, loc and ix in pandas python

There are different tasks can be performed using iloc and loc function in pandas,

  • Select row by using row index or row number in pandas with .iloc
  • select column based on the column position in pandas with iloc
  • select column based on column name in pandas using loc

 

Indexing in pandas python is done mostly with the help of iloc, loc and ix.  lets see an example of each .

iloc –  iloc is used for indexing or selecting based on position .i.e. by row number and column number

loc –   loc is used for indexing or selecting based on name .i.e. by row name and column name

ix – indexing can be done by both position and name using ix.

 

loc Vs iloc:

Indexing with iloc, loc and ix in pandas python 0

Create dataframe:

import pandas as pd
import numpy as np

#Create a DataFrame
d = {
    'Name':['Alisa','Bobby','Cathrine','Alisa','Bobby','Cathrine',
            'Alisa','Bobby','Cathrine','Alisa','Bobby','Cathrine'],
    'Exam':['Semester 1','Semester 1','Semester 1','Semester 1','Semester 1','Semester 1',
            'Semester 2','Semester 2','Semester 2','Semester 2','Semester 2','Semester 2'],
    
    'Subject':['Mathematics','Mathematics','Mathematics','Science','Science','Science',
               'Mathematics','Mathematics','Mathematics','Science','Science','Science'],
   'Score':[62,47,55,74,31,77,85,63,42,67,89,81]}

df = pd.DataFrame(d,columns=['Name','Exam','Subject','Score'])
df

so the resultant dataframe will be

Indexing with iloc, loc and ix in pandas python 1

 

 

Indexing with iloc:

.iloc [1:m, 1:n] –  is used to select or index rows based on their position from 1 to m rows and 1 to n columns

Select row by using row number in pandas with .iloc


# select first 2 rows
df.iloc[:2]
# or
df.iloc[:2,]

output:

Indexing with iloc, loc and ix in pandas python 2

 

 


# select 3rd to 5th rows
df.iloc[2:5]
# or 
df.iloc[2:5,]

output:

Indexing with iloc, loc and ix in pandas python 3

 

 


# select all rows starting from third row
df.iloc[2:]
# or
df.iloc[2:,]

output:

Indexing with iloc, loc and ix in pandas python 4

 

Select column by using column position in pandas with .iloc


# select first 2 columns

df.iloc[:,:2]

output:

Indexing with iloc, loc and ix in pandas python 5

 


# select 1st and 4thcolumn
df.iloc[:,[0,3]]

output:

Indexing with iloc, loc and ix in pandas python 6

 

 

Select values by using .iloc

Select 2nd row and 3rd column value

# Select 2nd row and 3rd column value

df.iloc[1,2]

output:

‘Mathematics’

 

 

indexing with loc :

loc [[Row_names],[ column_names]] –  is used to select or index rows or columns  based on their name


# select row by now name

df.loc[1]

In the dataframe df has default row names from 1 to 11. So df.loc[1] denotes selecting the row by row name 1, so output will be

Indexing with iloc, loc and ix in pandas python 7

 


# select value by row label and column label using loc

df.loc[[1,2,3,4,5],['Name','Score']]

output:

Indexing with iloc, loc and ix in pandas python 8

 

Indexing with ix

Indexing with ix is the combination of indexing with iloc and loc. ix is used for indexing based on the position and as well by names.

View a column in pandas


# Get all values of column ‘Score’

df.ix[:,'Score']

Output:

Indexing with iloc, loc and ix in pandas python 9

 

View a row based on row numbers


# Get 3rd & 4th row

df.ix[4:5,]

Output:

Indexing with iloc, loc and ix in pandas python 10

 

View the value based on row and column number


df.ix[3,2]

Output:

‘Science’

 

previous-small Indexing with iloc, loc and ix in pandas python                                                                                                           next_small Indexing with iloc, loc and ix in pandas python

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.