In this article, we will discuss multiple ways to select a row (or multiple rows) of pandas DataFrame by integer index.
Table of Contents
Preparing dataset for solution
To quickly get started, let’s create a sample dataframe to experiment. We’ll use the pandas library with some random data.
import pandas as pd import numpy as np # List of Tuples employees= [('Shubham', 'Data Scientist', 'Tech', 5), ('Riti', 'Data Engineer', 'Tech' , 7), ('Shanky', 'Program Manager', 'PMO' , 2), ('Shreya', 'Graphic Designer', 'Design' , 2), ('Aadi', 'Backend Developer', 'Tech', 11), ('Sim', 'Data Engineer', 'Tech', 4)] # Create a DataFrame object from list of tuples df = pd.DataFrame(employees, columns=['Name', 'Designation', 'Team', 'Experience'], index=["r1","r2","r3","r4","r5","r6"]) print(df)
Contents of the created dataframe are,
Name Designation Team Experience r1 Shubham Data Scientist Tech 5 r2 Riti Data Engineer Tech 7 r3 Shanky Program Manager PMO 2 r4 Shreya Graphic Designer Design 2 r5 Aadi Backend Developer Tech 11 r6 Sim Data Engineer Tech 4
Let’s see how to select a row from this DataFrame by index.
Select a row of Pandas DataFrame by integer index using iloc[]
The iloc[] is the method to select rows based on the row position (or number). For example, let’s say, we need to filter the second row from the above DataFrame.
# filter second row (index wise 1) print (df.iloc[1])
Output
Frequently Asked:
- How to delete first N columns of pandas dataframe
- Drop last row of pandas dataframe in python (3 ways)
- Add Column at a specific position in Pandas DataFrame
- Replace column values by condition in Pandas
Name Riti Designation Data Engineer Team Tech Experience 7 Name: r2, dtype: object
We have just passed the index in the iloc function and it returns the column wise values of the second row. In case, we wanted to select multiple rows, we can simply pass it in the iloc function again.
# filter consecutive rows print (df.iloc[2:4])
Output
Name Designation Team Experience r3 Shanky Program Manager PMO 2 r4 Shreya Graphic Designer Design 2
In case the rows are not consecutive, we can also pass the list of row positions to be filtered as below.
# filter defined rows print (df.iloc[[1,4,5]])
Output
Name Designation Team Experience r2 Riti Data Engineer Tech 7 r5 Aadi Backend Developer Tech 11 r6 Sim Data Engineer Tech 4
Here, the output contains all three rows that we have selected.
The complete example is as follows,
import pandas as pd import numpy as np # List of Tuples employees= [('Shubham', 'Data Scientist', 'Tech', 5), ('Riti', 'Data Engineer', 'Tech' , 7), ('Shanky', 'Program Manager', 'PMO' , 2), ('Shreya', 'Graphic Designer', 'Design' , 2), ('Aadi', 'Backend Developer', 'Tech', 11), ('Sim', 'Data Engineer', 'Tech', 4)] # Create a DataFrame object from list of tuples df = pd.DataFrame(employees, columns=['Name', 'Designation', 'Team', 'Experience'], index=["r1","r2","r3","r4","r5","r6"]) print(df) # filter second row (index wise 1) print (df.iloc[1]) # filter consecutive rows print (df.iloc[2:4]) # filter defined rows print (df.iloc[[1,4,5]])
Output:
Name Designation Team Experience r1 Shubham Data Scientist Tech 5 r2 Riti Data Engineer Tech 7 r3 Shanky Program Manager PMO 2 r4 Shreya Graphic Designer Design 2 r5 Aadi Backend Developer Tech 11 r6 Sim Data Engineer Tech 4 Name Riti Designation Data Engineer Team Tech Experience 7 Name: r2, dtype: object Name Designation Team Experience r3 Shanky Program Manager PMO 2 r4 Shreya Graphic Designer Design 2 Name Designation Team Experience r2 Riti Data Engineer Tech 7 r5 Aadi Backend Developer Tech 11 r6 Sim Data Engineer Tech 4
Select a row of Pandas DataFrame by integer index using loc[]
The loc[] function also works similarly to the iloc[] function, the only difference here is we need to pass the row names instead of position in the function call. Let’s quickly try to get the same output as above using the loc function.
# filter second row "r2" print (df.loc["r2"])
Output
Name Riti Designation Data Engineer Team Tech Experience 7 Name: r2, dtype: object
Here you go, we have similar output, i.e., all the column wise details of the second row. Also, to select multiple rows using loc function, again we need to pass the index names separated by a colon.
# filter consecutive rows print (df.loc["r3":"r4"])
Output
Name Designation Team Experience r3 Shanky Program Manager PMO 2 r4 Shreya Graphic Designer Design 2
We have filtered the third and fourth successfully. Note that, here both start (“r3”) and end (“r4”) are included while filtering from the DataFrame.
The complete example is as follows,
import pandas as pd import numpy as np # List of Tuples employees= [('Shubham', 'Data Scientist', 'Tech', 5), ('Riti', 'Data Engineer', 'Tech' , 7), ('Shanky', 'Program Manager', 'PMO' , 2), ('Shreya', 'Graphic Designer', 'Design' , 2), ('Aadi', 'Backend Developer', 'Tech', 11), ('Sim', 'Data Engineer', 'Tech', 4)] # Create a DataFrame object from list of tuples df = pd.DataFrame(employees, columns=['Name', 'Designation', 'Team', 'Experience'], index=["r1","r2","r3","r4","r5","r6"]) print(df) # filter second row "r2" print (df.loc["r2"]) # filter consecutive rows print (df.loc["r3":"r4"])
Output:
Name Designation Team Experience r1 Shubham Data Scientist Tech 5 r2 Riti Data Engineer Tech 7 r3 Shanky Program Manager PMO 2 r4 Shreya Graphic Designer Design 2 r5 Aadi Backend Developer Tech 11 r6 Sim Data Engineer Tech 4 Name Riti Designation Data Engineer Team Tech Experience 7 Name: r2, dtype: object Name Designation Team Experience r3 Shanky Program Manager PMO 2 r4 Shreya Graphic Designer Design 2
Summary
In this article, we have discussed multiple ways to select a row (or multiple rows) of pandas DataFrame by integer index. Thanks.