In this tutorial, we will learn how to obtain row labels from a dataframe. We will also see how to obtain row labels for a dataframe with custom row labels.
How to get row labels of a Pandas dataframe?

Dataframe row labels can be obtained using Pandas’ dataframe in-built index property. df.index
returns a RangeIndex object or an Index object, depending on whether the dataframe df
has a default index or a custom index. The underlying row labels can be obtained using df.index.values
. The syntax will be-
row_labels = df.index.values
Here,
df
— Pandas DataFrame.df.index
— returns RangeIndex object for a dataframe with default index, else returns an Index object.df.index.values
— returns a numpy array with row labels.
The df.index
attribute returns a RangeIndex object containing the recipe for an iterable over the index. The underlying row labels are obtained as a Numpy array using df.index.values.
Examples
Let’s understand the above syntax with some examples. For that, we will create a dataframe with the profiles of employees in a company.
import pandas as pd #Making a dictionary to pass it to dataframe d = {'Name': ["Adam", "Jean", "Smith"], 'Position': ["Manager", "Sr. Engineer", "Technical Lead"], 'Salary': [300000, 100000, 80000]} #DataFrame object is created. df = pd.DataFrame(d) #Display the dataframe df
Output:

This dataframe has a default index, as indicated by integral values for row labels.
Example 1: Get row labels of a Pandas DataFrame with default row labels.
As discussed earlier, we use df.index.values
to obtain default row labels for a dataframe df.
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.
#Get the default row labels of the dataframe default_row_labels = df.index.values print(default_row_labels)
Output:
[0 1 2]
The output is a numpy array starting at 0 and ending at 2. The default index for a dataframe always starts at 0 and increases by one for the subsequent rows.
Example 2: Get row labels of Pandas DataFrame with custom row labels.
Let’s set the column ‘Name’ as the index of the dataframe.
df.set_index('Name', inplace=True) print(df)
Output:
Position Salary Name Adam Manager 300000 Jean Sr. Engineer 100000 Smith Technical Lead 80000
Note here that we passed inplace=True
to the set_index()
function. Setting inplace=True
will modify the dataframe in place instead of returning a dataframe, the latter being the default behavior of most of the Pandas’ functions.
To obtain the row labels of a custom dataframe, we do the same as we did for a dataframe with default row labels.
#Get the custom row labels of the dataframe custom_row_labels = df.index.values print(custom_row_labels)
Output:
['Adam' 'Jean' 'Smith']
Here, we have custom row labels that we set earlier. The output is again a Numpy array.
Example 3: Get default row labels of Pandas DataFrame with custom row labels.
Even when the dataframe has custom row labels, we may need default row labels to work with slicing methods like iloc
.
For a given set of custom row labels, we can obtain the default row labels using the get_indexer method()
. We pass the list of custom row labels for which we need default row labels.
#Get the default row labels of the dataframe default_row_labels = df.index.get_indexer(['Adam', 'Smith']) print(default_row_labels)
Output:
[0 2]
Here, we have obtained the default row labels for the first and last rows. The first row has a default row label of 0 as always, while the last row has a default row label of 2 for this dataframe, as was seen in the first example too.
Summary
From this article, we have looked at how to:
- Get the row labels of a dataframe with the default row labels and obtain the row labels of a dataframe with custom row labels
- Fetch default row labels for a set of custom row labels.
You might also be interested in –
- Average for each row in Pandas Dataframe
- Pandas – Drop first n rows of a DataFrame
- Pandas – Drop first n rows of a DataFrame
- Pandas – Random Sample of Rows
- Reset Index in Pandas – With Example
- Pandas – Set Column as Index (With Examples)
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.