Pandas Dataframe Rename Index
Last Updated :
13 Jan, 2025
To rename the index of a Pandas DataFrame, rename() method is most easier way to rename specific index values in a pandas dataFrame; allows to selectively change index names without affecting other values.
Python
import pandas as pd
data = {'Name': ['John', 'Alice', 'Bob', 'Eve'],
'Age': [25, 30, 22, 35],
'Gender': ['Male', 'Female', 'Male', 'Female'],
'Salary': [50000, 55000, 40000, 70000]}
df = pd.DataFrame(data)
df.rename(index={0: 'Row1', 1: 'Row2'}, inplace=True)
print(df)
Output Name Age Gender Salary
Row1 John 25 Male 50000
Row2 Alice 30 Female 55000
2 Bob 22 Male 40000
3 Eve 35 Female 70000
Let’s explore various methods for renaming the index of a Pandas DataFrame, focusing on the rename_axis() method, direct assignment, the set_index() method, and multi-level indexing. For Other methods implementation we use the same dataset that we have used in above example.
1. Using rename axis
rename_axis() only changes the label of the axis and does not affect the data.
Python
# Rename the index axis to 'Employee ID'
df_renamed_index = df.rename_axis('Employee ID')
print(df_renamed_index)
Output Name Age Gender Salary
Employee ID
0 John 25 Male 50000
1 Alice 30 Female 55000
2 Bob 22 Male 40000
...
2. Renaming the Index Values Using set index
The set_index() method allows you to set one or more columns as the new index for the DataFrame.Use the set_index() method to set a column (e.g., "Name") as the new index.
Python
# Set 'Name' as the new index
df_with_name_index = df.set_index('Name')
print(df_with_name_index)
Output Age Gender Salary
Name
John 25 Male 50000
Alice 30 Female 55000
Bob 22 Male 40000
Eve 35 Female 70000
3. Renaming the Index In-Place Using index.name
If you want to directly rename the index of a DataFrame in place (without creating a new DataFrame), you can modify the name attribute of the index object.
Python
# Rename the index in place
df.index.name = 'Employee ID'
print(df)
Output Name Age Gender Salary
Employee ID
0 John 25 Male 50000
1 Alice 30 Female 55000
2 Bob 22 Male 40000
...
4. Resetting the Index Using reset index
If you’ve set a custom index using set_index() and want to revert back to the default integer index, you can use the reset_index() method. This method restores the default integer index and optionally keeps the previous index as a column.
Python
# Set 'Name' as index and then reset it
df_with_name_index = df.set_index('Name')
df_reset_index = df_with_name_index.reset_index()
print(df_reset_index)
Output Name Age Gender Salary
0 John 25 Male 50000
1 Alice 30 Female 55000
2 Bob 22 Male 40000
3 Eve 35 Female 70000
5. Setting Multi-level Index
Pandas allows for more advanced indexing, such as multi-level (hierarchical) indexing, where you can use multiple columns as the index. This is particularly useful for handling complex datasets.
Python
# Set 'Gender' and 'Age' as multi-level index
df_multi_index = df.set_index(['Gender', 'Age'])
print(df_multi_index)
Output Name Salary
Gender Age
Male 25 John 50000
Female 30 Alice 55000
Male 22 Bob 40000
Female 35 Eve 70000
Here are some key takeaways:
- Use rename() to change specific index values and set_index() can set a column as the new index in dataframe.
- reset_index() reverts to the default integer index.
- we use rename_axis() to that renames the axis labels
Similar Reads
Pandas DataFrame.reset_index() In Pandas, reset_index() method is used to reset the index of a DataFrame. By default, it creates a new integer-based index starting from 0, making the DataFrame easier to work with in various scenarios, especially after performing operations like filtering, grouping or multi-level indexing. Example
3 min read
Reset Index in Pandas Dataframe Letâs discuss how to reset the index in Pandas DataFrame. Often We start with a huge data frame in Pandas and after manipulating/filtering the data frame, we end up with a much smaller data frame. When we look at the smaller data frame, it might still carry the row index of the original data frame.
6 min read
Pandas Dataframe Index Index in pandas dataframe act as reference for each row in dataset. It can be numeric or based on specific column values. The default index is usually a RangeIndex starting from 0, but you can customize it for better data understanding. You can easily access the current index of a dataframe using th
3 min read
Pandas DataFrame index Property In Pandas we have names to identify columns but for identifying rows, we have indices. The index property in a pandas dataFrame allows to identify and access specific rows within dataset. Essentially, the index is a series of labels that uniquely identify each row in the DataFrame. These labels can
6 min read
Python | Pandas DataFrame.set_index() Pandas DataFrame.set_index() method sets one or more columns as the index of a DataFrame. It can accept single or multiple column names and is useful for modifying or adding new indices to your DataFrame. By doing so, you can enhance data retrieval, indexing, and merging tasks.Syntax: DataFrame.set_
3 min read
Python | Pandas Dataframe.rename() Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas rename() method is used to rename any index, column or row. Renaming of column
3 min read
How to rename columns in Pandas DataFrame In this article, we will see how to rename column in Pandas DataFrame. The simplest way to rename columns in a Pandas DataFrame is to use the rename() function. This method allows renaming specific columns by passing a dictionary, where keys are the old column names and values are the new column nam
4 min read
Python | Pandas dataframe.rename_axis() Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. dataframe.rename_axis() is used to rename the axes of the index or columns in datafram
2 min read
Name and Rename Attributes in Pandas In the world of data science and analysis, the Pandas library in Python plays a crucial role that cannot be neglected when we talk about data manipulation and exploration. Data frames and series are the core structures of the Pandas library. Data frames are used to describe 2-dimensional data struct
4 min read
Python | Pandas Index.to_frame() Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas Index.to_frame() function create a dataFrame from the given index with a column
2 min read