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:
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:
.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:
# select 3rd to 5th rows df.iloc[2:5] # or df.iloc[2:5,]
output:
# select all rows starting from third row df.iloc[2:] # or df.iloc[2:,]
output:
Select column by using column number in pandas with .iloc
# select first 2 columns df.iloc[:,:2]
output:
# select 1st and 4thcolumn df.iloc[:,[0,3]]
output:
Select values by using .iloc
Select 2nd row and 3rd column value
# Select 2nd row and 3rd column value df.iloc[1,2]
output:
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
# select value by row label and column label using loc df.loc[[1,2,3,4,5],['Name','Score']]
output:
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:
View a row based on row numbers
# Get 3rd & 4th row df.ix[4:5,]
Output:
View the value based on row and column number
df.ix[3,2]
Output: