VOOZH about

URL: https://www.geeksforgeeks.org/pandas/pandas-dataframe-rename-index/

⇱ Pandas Dataframe Rename Index - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Pandas Dataframe Rename Index

Last Updated : 23 Jul, 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.


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.


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.


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.


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.


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.


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
Comment

Explore