pandas
dataframe
index renaming
python
data manipulation

Rename Pandas DataFrame Index

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

In pandas, "rename the index" can mean two different things. You might want to rename the index labels themselves, such as changing row1 to north, or you might want to rename the index axis name, such as changing the label from None to customer_id. Knowing which one you need determines which pandas method to use.

Rename Index Labels with rename

If you want to change one or more actual index values, use DataFrame.rename with the index argument.

python
1import pandas as pd
2
3df = pd.DataFrame(
4    {"sales": [120, 150, 90]},
5    index=["row1", "row2", "row3"]
6)
7
8renamed = df.rename(index={"row1": "north", "row3": "south"})
9print(renamed)

Output:

text
1       sales
2north    120
3row2     150
4south     90

This is the best option when you only need to update selected labels and want to leave the rest unchanged.

Replace the Entire Index at Once

If every index label should change and you already have the full replacement list, assign directly to df.index.

python
1import pandas as pd
2
3df = pd.DataFrame(
4    {"sales": [120, 150, 90]},
5    index=["row1", "row2", "row3"]
6)
7
8df.index = ["north", "east", "south"]
9print(df)

Direct assignment is concise, but the replacement list must match the DataFrame length exactly. If it does not, pandas raises a length mismatch error.

This direct approach is often the clearest option after you sort, filter, or rebuild a DataFrame and already know the exact final labels you want. It is less convenient when only a few rows need new names, which is why rename(index=...) remains the better tool for partial updates.

Rename the Index Axis Name

Sometimes the labels are fine, but the index itself needs a descriptive name. That is a separate operation. Use rename_axis or assign to df.index.name.

python
1import pandas as pd
2
3df = pd.DataFrame(
4    {"sales": [120, 150, 90]},
5    index=["north", "east", "south"]
6)
7
8df = df.rename_axis("region")
9print(df)

Output:

text
1        sales
2region       
3north     120
4east      150
5south      90

This matters because many pandas operations preserve index names in joins, exports, and reshaping steps. A meaningful axis name makes downstream code easier to read.

Rename MultiIndex Levels

If your DataFrame uses a MultiIndex, you may want to rename the level names rather than the label values. rename_axis accepts a list for that case:

python
1import pandas as pd
2
3index = pd.MultiIndex.from_tuples(
4    [("north", 2024), ("south", 2024)],
5    names=["old_region", "old_year"]
6)
7
8df = pd.DataFrame({"sales": [120, 90]}, index=index)
9df = df.rename_axis(["region", "year"])
10
11print(df)

That changes the names of the levels, not the tuple values stored in each row.

In-Place Versus New DataFrame

Many pandas methods return a new DataFrame by default. If you write:

python
renamed = df.rename(index={"row1": "north"})

the original df is unchanged unless you reassign it or use inplace=True. Many teams prefer reassignment because it is easier to reason about in method chains and tests.

Common Pitfalls

One common mistake is confusing index labels with the index name. rename(index=...) changes actual row labels, while rename_axis(...) changes the axis name shown above the index.

Another mistake is assigning a full replacement list to df.index with the wrong length. Pandas requires the number of new labels to match the number of rows exactly.

Developers also sometimes expect rename to mutate the DataFrame automatically. Unless you use inplace=True or assign the result back, the original object remains unchanged.

Finally, be careful with MultiIndex objects. Renaming level names and renaming level values are separate operations and use different patterns.

Summary

  • Use df.rename(index=...) to rename selected index labels.
  • Assign to df.index when you want to replace the entire index.
  • Use rename_axis or df.index.name to rename the index axis name.
  • 'MultiIndex level names can also be renamed with rename_axis.'
  • The main source of confusion is mixing up label changes and axis-name changes.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.