Skip to Content

Pandas – Get Rows by their Index and Labels

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.

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:

resulting pandas dataframe with student information

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:

selected rows from the above dataframe

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:

selected rows from the above dataframe

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:

selected rows from the above dataframe

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 –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Authors

  • Piyush

    Piyush is a data scientist passionate about using data to understand things better and make informed decisions. In the past, he's worked as a Data Scientist for ZS and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

  • Tushar Mahuri