Skip to Content

Get Row Labels of a Pandas DataFrame.

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?

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:

pandas dataframe with employee information

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.

#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 –


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