Basically, a Pandas DataFrame has two indices. These indices are distinguished by their axis. The row index is an index that is located along axis 0 (horizontal), whereas the column index is an index that is located along axis 1 (vertical).
In this article, we will use iloc[] and loc[] functions to get the rows from the DataFrame. We need to specify the row and column ranges (start and end locations along the columns or rows). The location-based indexing can be used to query the Pandas DataFrames.
Syntax – iloc[]
Parameter
Index Position: Index number of rows (numeric) or list of integers.
Note: If the Index doesn’t present in the DataFrame, IndexError – “out of bounds” will be thrown.
Example – Retrieve Specific Row
Create Pandas DataFrame named – actual_details with 5 rows and 2 columns – ‘class’,’syllabus_date’, and ‘Hours’.
Now, use the iloc[] function to:
- Return the row present at index-2
- Return the row present at index-0
- Return the row present at index-4
# Create the data frame that has 5 rows with 2 columns
actual_details = pandas.DataFrame({‘class’: [‘open source’,’linux’,’algorithms’,’botany’,’social studies’],
‘Hours’:[12,8,9,12,12]})
print(actual_details.iloc[2])
print()
print(actual_details.iloc[0])
print()
print(actual_details.iloc[4])
Output
Hours 9
Name: 2, dtype: object
class open source
Hours 12
Name: 0, dtype: object
class social studies
Hours 12
Name: 4, dtype: object
Explanation
- At index-2, the ‘class’ is ‘algorithms’, and the ‘Hours’ is ‘9’.
- At index-0, the ‘class’ is ‘open source’, and the ‘Hours’ is ‘12’.
- At index-4, the ‘class’ is ‘social studies’, and the ‘Hours’ is ‘12’.
Scenario 1 – Retrieve Multiple Rows
It can be possible to retrieve multiple rows simultaneously using iloc[]. We need to pass the row indices in a list to the iloc[].
Syntax
Example
Create Pandas DataFrame named – actual_details with 5 rows and 2 columns – ‘class’,’syllabus_date’, and ‘Hours’.
Now, use iloc[] to return the rows present at index-2,0 and 4.
# Create the dataframe that has 5 rows with 2 columns
actual_details = pandas.DataFrame({‘class’: [‘open source’,’linux’,’algorithms’,’botany’,’social studies’],
‘Hours’:[12,8,9,12,12]})
# Retrieve row indices 2,0 and 4
print(actual_details.iloc[[2,0,4]])
Output
2 algorithms 9
0 open source 12
4 social studies 12
Scenario 2 – Retrieve Multiple Rows With Specific Columns
Sometimes, we want to retrieve the data only for specific columns instead of retrieving the rows of the data with all the columns in the given DataFrame. Like row indexes in a Pandas DataFrame, the column position also serves as a column index.
Syntax
Example
Create Pandas DataFrame nam–d – actual_details with 5 rows and 2 colum–s – ‘class’,’syllabus_date’, and ‘Hours’.
Now, use the iloc[] function to return the rows present at index-2,0 and 4 for column-class and Hours separately.
# Create the dataframe that has 5 rows with 2 columns
actual_details = pandas.DataFram‘({'cl’ss'‘ ['open sou’c’','li’u’','algorit’m’','bot’n’','social stud’es'],
‘ 'Ho’rs':[12,8,9,12,12]})
# Retrieve row indices 2,0 and 4 for class column
print(actual_details.iloc[[2,0,4],[0]])
print()
# Retrieve row indices 2,0 and 4 for Hours column
print(actual_details.iloc[[2,0,4],[1]])
Output
4 2 algorith open source
4 social studies
Hours
4 2 12
4 12
Explanation
You can see that the rows were returned only for a particular column. Here, class represents index-0 and Hours represent index-1.
Like the iloc[] function, we can specify the ranges or multiple labels as a list inside the loc[] function to retrieve multiple rows.
Synt–x – loc[]
Parameter
Index: Index label of rows or list of labels.
Example–1 – Retrieve Specific Row
Create Pandas DataFrame nam–d – actual_details with 5 rows and 2 colum–s – ‘class’, ‘syllabus_date’, and ‘Hours’ along with indices as [‘c1’,‘c2’,‘c3’,‘c4’,‘c5’].
Now, use the iloc[] function to:
- Return the row present at ‘c3’
- Return the row present at ‘c1’
- Return the row present at ‘c5’
# Create the dataframe that has 5 rows with 2 columns
actual_details = pandas.DataFrame({'class': ['open source','linux','algorithms','botany','social studies'],
'Hours':[12,8,9,12,12]},index=['c1','c2','c3','c4','c5'])
# Retrieve row indices - 'c3','c1' and 'c5' separately
print(actual_details.loc['c3'])
print()
print(actual_details.loc['c1'])
print()
print(actual_details.loc['c5'])
Output
Hours 9
Name: c3, dtype: object
class open source
Hours 12
Name: c1, dtype: object
class social studies
Hours 12
Name: c5, dtype: object
Explanation
- At index-’c3’, the class is ‘algorithms’, and ‘Hours is 9.
- At index-’c1’, the class is ‘open source’, and ‘Hours’ is 12.
- At index-’c5’, the class is ‘social studies’, and ‘Hours’ is 12.
Example 2 – Retrieve Multiple Rows
Create Pandas DataFrame named – actual_details with 5 rows and 2 columns – ‘class’, ‘syllabus_date’, and ‘Hours’ along with indices as [‘c1’,‘c2’,‘c3’,‘c4’,‘c5’].
Now, use the iloc[] function to return the row present at ’c3’, ‘c1’, and ‘c5’.
# Create the dataframe that has 5 rows with 2 columns
actual_details = pandas.DataFrame({'class': ['open source','linux','algorithms','botany','social studies'],
'Hours':[12,8,9,12,12]},index=['c1','c2','c3','c4','c5'])
# Retrieve row indices - 'c3','c1' and 'c5' at a time
print(actual_details.loc[['c3','c1','c5']])
Output
c3 algorithms 9
c1 open source 12
c5 social studies 12
Example 3 – Retrieve Multiple Rows With Specific Column
Create Pandas DataFrame named – actual_details with 5 rows and 2 columns – ‘class’, ‘syllabus_date’, and ‘Hours’ along with indices as [‘c1’,‘c2’,‘c3’,‘c4’,‘c5’].
Now, use the iloc[] function to return the row present at ’c3’, ‘c1’, and ‘c5’ only for the ‘class’ column.
# Create the data frame that has 5 rows with 2 columns
actual_details = pandas.DataFrame({'class': ['open source','linux','algorithms','botany','social studies'],
'Hours':[12,8,9,12,12]},index=['c1','c2','c3','c4','c5'])
# Retrieve row indices - 'c3','c1' and 'c5' at a time for class column
print(actual_details.loc[['c3','c1','c5'],['class']])
Output:
c3 algorithms
c1 open source
c5 social studies
Explanation
You can see that the rows were returned only for a particular column, i.e., class.
Conclusion
In this article, we discussed how to get the DataFrame rows using the index in Pandas. After going through this article, you may be able to retrieve the DataFrame rows using the different functions. We implemented a few examples to teach you how to use the iloc[] function to retrieve single or multiple rows from the DataFrame by the numeric index and the loc[] function to get single or multiple rows from the DataFrame by non-numeric index.