pandas
data manipulation
index column
python
data analysis

How to get/set a pandas index column title or name?

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, the row index can have a name, and that name is separate from the ordinary DataFrame column labels. If you want to get or set the index title, the main attribute to know is df.index.name for a single index and df.index.names for a MultiIndex.

Getting the Index Name

For a normal single-level index, use:

python
1import pandas as pd
2
3df = pd.DataFrame({"value": [10, 20, 30]})
4df.index.name = "row_id"
5
6print(df.index.name)

This prints row_id.

If the DataFrame has no explicit index name yet, df.index.name returns None.

Setting the Index Name

The simplest way to set it is direct assignment:

python
df.index.name = "row_id"
print(df)

That changes the label shown for the index in many outputs and exported formats.

You can also set it while creating or transforming the DataFrame. For example, after using set_index:

python
1df = pd.DataFrame({
2    "id": [101, 102, 103],
3    "value": [10, 20, 30]
4}).set_index("id")
5
6df.index.name = "customer_id"

This changes the index name from id to customer_id without altering the underlying index values.

Using rename_axis

A more pipeline-friendly option is rename_axis, which returns a new object unless you assign it back:

python
df = df.rename_axis("row_id")

This is especially nice in method chains:

python
1df = (
2    pd.DataFrame({"id": [1, 2], "value": [4, 5]})
3      .set_index("id")
4      .rename_axis("item_id")
5)

Use this when you want a more declarative style.

MultiIndex Case

If your DataFrame uses multiple index levels, the attribute changes from singular to plural:

python
1arrays = [["A", "A", "B"], [1, 2, 1]]
2index = pd.MultiIndex.from_arrays(arrays, names=["group", "position"])
3df = pd.DataFrame({"value": [10, 20, 30]}, index=index)
4
5print(df.index.names)

To rename all levels:

python
df.index.names = ["bucket", "slot"]

Or with rename_axis:

python
df = df.rename_axis(index=["bucket", "slot"])

Index Name vs Column Name

This is a common source of confusion. The index name is not the same thing as a normal column header.

For example:

python
print(df.columns)
print(df.index.name)

These represent different metadata:

  • 'df.columns labels actual columns'
  • 'df.index.name labels the row index axis'

If you reset the index, the index values may become a column and then the name can appear as a column label:

python
reset_df = df.reset_index()

That often makes the distinction finally visible.

Another useful detail is that index names improve readability in merges, exports, and notebook output. A named index is much easier to reason about than an unlabeled one when multiple transformation steps are chained together.

Persistence in Files

Index names are often preserved when exporting to CSV, Excel, or other formats, but only if you include the index in the export. For example:

python
df.to_csv("output.csv", index=True)

If you write with index=False, the index and its name are omitted entirely.

Common Pitfalls

The biggest mistake is confusing df.index.name with df.columns.name. They are different pieces of metadata.

Another issue is using df.index.names on a single-level index or df.index.name on a MultiIndex without realizing the difference.

People also sometimes expect rename on columns to affect the index title. It will not. Index naming is handled through the index metadata APIs.

Finally, do not forget that some exports drop the index if index=False is used, which makes the index name disappear as well.

Summary

  • Use df.index.name to get or set the name of a single-level index.
  • Use df.index.names for a MultiIndex.
  • 'rename_axis is a clean alternative for method chaining.'
  • The index name is different from ordinary column names.
  • If you omit the index during export, its name disappears too.

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.