How to Rename the Index of a Pandas DataFrame: Complete Guide with Source Code

You can rename the index of a pandas DataFrame using DataFrame.rename() to map existing labels to new values, DataFrame.rename_axis() to change the index name metadata, DataFrame.set_index() to replace the index with column data, or DataFrame.reset_index() to move the index back into a column.

Working with the pandas-dev/pandas repository, you have several built-in methods to modify the row identifiers of a DataFrame. Whether you need to update specific index labels, change the axis name, or completely rebuild the index structure, the implementation in pandas/core/frame.py provides flexible, non-destructive operations that return new objects by default.

Rename Specific Index Labels with DataFrame.rename()

The DataFrame.rename() method allows you to change individual index values by supplying a mapper—either a dictionary, a function, or a Series. According to the source code in pandas/core/frame.py, this method forwards to DataFrame._rename, which internally calls Index.rename for the row axis.

The method signature in pandas/core/frame.py (around line 6124) is:

def rename(self, mapper=None, *, index=None, columns=None,
           axis=None, copy=True, inplace=False, level=None, 
           errors='ignore'):
    ...

Using a Dictionary Mapping

Pass a dictionary to the index parameter to map specific old labels to new ones:

import pandas as pd

df = pd.DataFrame(
    {"city": ["Paris", "Berlin", "Tokyo"],
     "population": [2.2, 3.6, 13.9]},
    index=["FR", "DE", "JP"]
)

# Rename specific index labels

df_renamed = df.rename(index={"FR": "France", "DE": "Germany"})
print(df_renamed)

Using a Callable Function

You can also pass a function that transforms each label:


# Prepend text to each index label

df_func = df.rename(index=lambda x: f"Country-{x}")
print(df_func)

Change the Index Name with DataFrame.rename_axis()

While rename() modifies the labels themselves, DataFrame.rename_axis() changes the name of the index axis (the .name attribute). This is useful for setting meaningful metadata like "date" or "country_code" without altering the actual row identifiers.

The implementation in pandas/core/frame.py (around line 6165) shows:

def rename_axis(self, mapper=None, *, index=None, columns=None,
                axis=None, copy=True, inplace=False):
    ...

Under the hood, this method invokes Index.rename_axis on the underlying index object located in pandas/core/indexes/base.py.

Setting the Axis Name


# Change the index name metadata

df_named = df.rename_axis("country_code")
print(df_named.index.name)  # Output: "country_code"

Replace the Entire Index with DataFrame.set_index()

To completely replace the current index with new values—such as converting a column into the index or applying a new Index object—use DataFrame.set_index(). This is the appropriate method when you need to rebuild the index structure rather than rename existing labels.


# Replace index with a new Index object

df_new_index = df.set_index(pd.Index(["FRN", "DEN", "JPN"], name="iso"))
print(df_new_index)

Move Index to Column with DataFrame.reset_index()

When you need to undo a previous index assignment or move the current index back into the DataFrame as a regular column, use DataFrame.reset_index(). This effectively reverses set_index() operations.


# Move index back to a column

df_reset = df_new_index.reset_index()
print(df_reset)

# Or drop the index entirely

df_dropped = df_new_index.reset_index(drop=True)

In-Place vs. Copy Operations

All four methods are non-destructive by default, returning new DataFrame objects. To modify the existing object without assignment, set inplace=True:

df.rename(index={"FR": "France"}, inplace=True)

Alternatively, assign the result back to your variable. The copy parameter controls whether underlying data is copied; when copy=True (default), modifications to the new DataFrame do not affect the original.

Summary

  • DataFrame.rename(): Changes individual index labels via mapping or functions; forwards to Index.rename in pandas/core/indexes/base.py.
  • DataFrame.rename_axis(): Modifies the index name metadata (.name attribute), not the labels themselves.
  • DataFrame.set_index(): Replaces the entire index with column data or a new Index object.
  • DataFrame.reset_index(): Moves the current index back into the DataFrame as a column or drops it.
  • All methods support inplace=True for mutation and are implemented in pandas/core/frame.py.

Frequently Asked Questions

What is the difference between rename and rename_axis in pandas?

DataFrame.rename() changes the actual values of the index labels (the row identifiers), while DataFrame.rename_axis() only changes the name attribute of the index axis (the metadata label for the index itself). Use rename() when you want "FR" to become "France", and use rename_axis() when you want the index to have the name "country_code".

How do I rename a MultiIndex in pandas?

Use DataFrame.rename() with the level parameter to target specific levels of a MultiIndex. For example, df.rename(index={"old": "new"}, level=0) renames labels only in the first level of the index hierarchy. You can also use rename_axis() to change the names of specific levels by passing the mapper and axis parameters.

Can I rename index labels in place without creating a copy?

Yes. Pass inplace=True to DataFrame.rename(), DataFrame.rename_axis(), DataFrame.set_index(), or DataFrame.reset_index(). This modifies the existing DataFrame object rather than returning a new one. Note that the default behavior (inplace=False, copy=True) creates a copy to preserve the original data.

How do I rename the index after using set_index()?

After using set_index(), you can rename the new index labels using df.rename(index={...}) or change the index name using df.rename_axis("new_name"). If you want to replace the index again with different data, simply call set_index() again with the new column or Index object.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →