Rename Index of pandas DataFrame in Python (2 Examples)

 

This article explains how to change the indices of a pandas DataFrame in the Python programming language.

The article consists of two examples for the modification of the indices of a pandas DataFrame. More precisely, the article is structured as follows:

Let’s take a look at some Python codes in action.

 

Example Data & Software Libraries

In order to use the functions of the pandas library, we first need to load pandas:

import pandas as pd                               # Import pandas library in Python

As a next step, we’ll also need to create some example data:

data = pd.DataFrame({'x1':[6, 7, 3, 7, 5, 9],    # Create example DataFrame
                     'x2':['a', 'x', 'a', 's', 'a', 'b'],
                     'x3':range(7, 1, - 1)})
print(data)                                      # Print example DataFrame
#    x1 x2  x3
# 0   6  a   7
# 1   7  x   6
# 2   3  a   5
# 3   7  s   4
# 4   5  a   3
# 5   9  b   2

As you can see based on the previous Python console output, our exemplifying data has six rows and three columns. The indices range from 0 to 5 and the index does not have a name yet.

 

Example 1: Rename Index of pandas DataFrame

Example 1 demonstrates how to rename the index of a pandas DataFrame.

For this, we can use the index and names attributes of our data set:

data.index.names = ['index_name']                # Rename index
print(data)                                      # Print updated DataFrame
#             x1 x2  x3
# index_name           
# 0            6  a   7
# 1            7  x   6
# 2            3  a   5
# 3            7  s   4
# 4            5  a   3
# 5            9  b   2

Have a look at the previous console output: The index name of our data matrix has been changed to index_name.

 

Example 2: Convert Column to Index of pandas DataFrame

Example 2 illustrates how to change the index values of a pandas DataFrame by using a variable of this DataFrame as index.

The following Python syntax transforms the column x3 to the index of our DataFrame:

data = data.set_index('x3')                      # Column as indices
print(data)                                      # Print updated DataFrame
#     x1 x2
# x3       
# 7    6  a
# 6    7  x
# 5    3  a
# 4    7  s
# 3    5  a
# 2    9  b

 

Video & Further Resources on this Topic

Would you like to learn more about the renaming of indices of a pandas DataFrame? Then you might have a look at the following video that I have published on my YouTube channel. I’m explaining the topics of this post in some more detail.

 

 

As an additional resource, you might have a look at the following video on the YouTube channel of Noureddin Sadawi. He’s explaining in another example how to rename the index of a pandas DataFrame:

 

 

In addition, you might have a look at some of the other posts on my website:

 

In summary: You have learned in this article how to change the index name of a pandas DataFrame in Python. Please let me know in the comments, in case you have any further questions.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top