The pandas library in Python comes with a number of useful methods to help you work with and manipulate tabular data. In this tutorial, we will look at how to get the rows of a pandas dataframe by their respective index with the help of some examples.
Access Rows in a pandas dataframe
There are two ways to access the rows of a pandas dataframe –
Using the row index
You can access one or more rows of a pandas dataframe by its index using the iloc
property of the dataframe. The iloc
property in a pandas dataframe lets you access rows and/or columns using their indices.
The following is the syntax –
# select row with index i df.iloc[i] # select rows with index i, j, and k df.iloc[[i, j, k]]
Using the row label
You can use the pandas dataframe loc
property to access one or more rows of a dataframe by their row labels. The loc
property in a pandas dataframe lets you access rows and/or columns using their respective labels.
The following is the syntax.
# select row with label l df.loc[l] # select rows with labels l, m, and n df.loc[[l, m, n]]
Examples
Let’s now look at some examples of using the above syntax.
First, we will create a pandas dataframe that we will be using throughout this tutorial.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
import pandas as pd # student data data = { "Age": [18, 17, 21, 18, 20], "Major": ["Arts", "Engineering", "Maths", "Physics", "Drama"] } # create dataframe with specified row labels df = pd.DataFrame(data, index=["Tim", "Ram", "Hari", "Sita", "Emma"]) # display the dataframe df
Output:
Here, we created a dataframe with information about some students in a university. The dataframe has “Age” and “Major” columns and the row labels represent the individual student’s name.
Example 1: Select Rows Based on their Integer Indices
Rows and columns are indexed starting from 0 (by default) in a pandas dataframe. As mentioned above, we can use the iloc
property to access rows using their integer indices.
Let’s select the row with the index 2 (the 3rd row) in the dataframe.
df.iloc[2]
Output:
Age 21 Major Maths Name: Hari, dtype: object
You can also use iloc
to select multiple rows by their integer indices. For this, use a list of row indices to access inside the iloc
square brackets.
Let’s select rows with integer index 1 and 3
df.iloc[[1, 3]]
Output:
Or we could select all rows in a range: rows from index 1 to 4 using a slice operation. Note that the end index is not inclusive.
df.iloc[1:4]
Output:
Example 2: Select Rows Based on thier Labels
Let’s now look at some examples of using the .loc
property. .loc
property is used to select rows and columns based on their labels. For example, if you want to select the row with a label ‘abc’, you would directly use df.loc['abc']
.
Let’s get the row with the label “Hari”.
# get row with row label df.loc["Hari"]
Output:
Age 21 Major Maths Name: Hari, dtype: object
Similarly, you can select multiple rows using their row labels. For example, let’s select the rows with labels “Ram” and “Sita”.
df.loc[["Ram", "Sita"]]
Output:
Summary
In this tutorial, we looked at how to get the rows of a pandas dataframe using the row indices and the row labels.
- Use the
iloc
proptery of the dataframe to select rows based on their integer index. - Use the
loc
proptery of the dataframe to select rows based on their labels.
You might also be interested in –
- How to access a Column in Pandas?
- Get Rows using Datetime Index in Pandas
- Get Rows with NaN values in Pandas
- Pandas – Delete rows based on column values
- Pandas – Select first n rows of a DataFrame
- Pandas – Random Sample of Rows
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.