Pandas Dateframe Index
Learn about pandas index and how to use it to access rows, how to define custom row labels and more with practical examples
Introduction
In a dataframe, we have huge number of data records. There must be something unique for each data record so that we can access it distinctly. You can use the pandas dataframe index for this. They are referred as row names or index names also in general.
By default, these row index labels are integers ranging from zero to one less than the total number of rows in the pandas DataFrame. However, user-defined row labels can also be used to identify the rows of the pandas DataFrame.
In this article, you will learn about the different methods of accessing the row index labels of a pandas DataFrame followed by some practical tips of using them.
Creating a pandas DataFrame
Let’s create a simple pandas dataframe to undertsand the concept of index.

Access index using the DataFrame.index method
To access the row labels use the command DataFrame.index.
Here, the above output states that the indices are a range of integers that starts from zero and stops before ten.
To view the actual row labels, print the indices as a list.
Using the DataFrame.index.values method
To access the values of the row indices as an array, use the command DataFrame.index.values.
Iterating over the indices
You can use a loop to iterate over the indices. Let’s see how to print each row label in a new line using a for loop.
Using the tolist() method
The .tolist() method is used to convert arrays and pandas objects to a list.
You can use this method if you wish to access the row labels as a list.
Using the DataFrame.axes method
The DataFrame.axes method can be used to access both rows and column labels. Pass the value 0 as the argument to access the row labels using this method.
You can also use this method in conjunction with the tolist() method to print the values of the row labels as a list.
Accessing row labels of a pandas dataframe based on specific conditions
You can also set some criteria for viewing the row labels of only those rows that meet that criteria.
In this case, if you wish to see only those row labels where the values of the Currently at column is ‘Apple’, you can do so in the following manner:
Passing a list of user-defined row indices
If you wish to pass a list of custom row labels, you can do so by passing the values as a list to the index parameter while creating the pandas DataFrame.
Let’s understand this using the below example. We have created a list named ‘ind’ that has the custom row labels of Nationalities and the data is in the form a dictionary.
Now we have created the pandas dataframe as desired. Next, let us access records using the custom index names or row labels
User-defined index: Index(['British', 'American', 'Russian', 'French', 'Canadian', 'German',
'German', 'American', 'Canadian', 'Slovak'],
dtype='object')
Practical Tips
- Keep in mind that the
DataFrame.indexmethod returns an index object and not a Series object .
- The pandas
DataFrame.index.valuesmethod returns a numpy array object.
- Although duplicate values are allowed to be used as row index in pandas, it is generally not recommended to do so as duplicate row labels make it difficult to differentiate between two different rows
For more information, you can check out the pandas official documentation
Test Your Knowledge
Q1: Duplicate values in user-defined row labels is allowed. True or False?
Answer:
Answer: True
Q2: Which method is used to return the row indices as a numpy array?
Answer:
Answer: DataFrame.index.values
Q3: Find out the error in the given code and write the correct code to print the row labels:
df.axes[1].tolist
Answer:
Answer: df.axes[0].tolist()
Q4: You have the following DataFrame df:
Write the code to view only those row labels where the type is ‘Part-time Employee’
Answer:
Answer: df.index[df['type']=='Part-time Employee']
This article was contributed by Shreyansh B and Shri Varsheni