Inspect df.index.name (or df.index.names for a MultiIndex) to read the current index label, and call df = df.rename_axis("new_name") (or assign df.index.name = "new_name" directly) to rename it—preferring df.rename_axis(...) when chaining methods or updating multiple MultiIndex level names cleanly without mutating shared index references.
Basic Example
import pandas as pd
df = pd.DataFrame(
{"revenue": [1200, 1850, 940]},
index=pd.Index(["2026-01", "2026-02", "2026-03"], name="month"),
)
# 1. Get the current index name (`None` if unnamed):
print(df.index.name)
# Output: month
# 2. Recommended: Rename index via `.rename_axis()` (chainable, non-mutating)
df = df.rename_axis("billing_period")
print(df.index.name)
# Output: billing_period
print(df)
# Output:
# revenue
# billing_period
# 2026-01 1200
# 2026-02 1850
# 2026-03 940
# 3. Clear / remove the index name completely (set back to `None`):
df_unnamed = df.rename_axis(None)
print(df_unnamed.index.name)
# Output: None
Key Details & Edge Cases
df.rename()renames index labels/values, NOT the index name:- A common mistake is calling
df.rename(index={"old": "new"}), which replaces row keys inside the index rather than the index’s metadata name. Usedf.rename_axis("new_name")(ordf.index.name = "new_name") to change the header shown above the index column.
- A common mistake is calling
- Getting and renaming
MultiIndexlevel names (df.index.names&df.rename_axis([...])):- On a
MultiIndex,df.index.namealways returnsNone; read the tuple/list of level names viadf.index.names(e.g.,FrozenList(['region', 'year'])). - Rename all levels at once by passing a list (
df = df.rename_axis(["territory", "fiscal_year"])) or rename a single level by mapping dictionary (df = df.rename_axis(index={"year": "fiscal_year"})).
- On a
- Renaming the column axis name (
columns.name) with the same method:.pivot_table()and.crosstab()often leave a lingering column-axis name (such as"product"displayed above the column headers). Clear or rename it withdf = df.rename_axis(columns=None)(ordf = df.rename_axis(index="row_id", columns="metric")).
- Why
df.rename_axis("new_name")is safer thandf.index.name = "new_name"on sliced DataFrames:- Assigning
df.index.name = "new_name"mutates theIndexobject in place—if two DataFrames share the sameIndexinstance in memory, mutating.index.namechanges both DataFrames’ index names and can trigger warnings on views.df.rename_axis("new_name")returns a new DataFrame reference with updated metadata and integrates cleanly into method chains (df.groupby("region").sum().rename_axis("sales_region").reset_index()).
- Assigning
- Renaming the index during
.reset_index()in Pandas 1.5+ (names=):- If your goal is to turn an unnamed index into a named regular column immediately, pass
names="row_id"directly todf.reset_index(names="row_id")instead of renaming the index first.
- If your goal is to turn an unnamed index into a named regular column immediately, pass
Comparison of Index-Name Inspection & Renaming Methods in Pandas
| Operation / Method | Works on MultiIndex? |
Mutates In-Place or Returns New DataFrame? | Supports Method Chaining? | Best Used When |
|---|---|---|---|---|
df.index.name / df.index.names |
.name (1D) / .names (MultiIndex) |
Read-only property lookup | N/A | Checking the current index name (returns None if unnamed). |
df = df.rename_axis("new_name") (Recommended) |
Yes (pass list ["a", "b"] or dict {"old": "new"}) |
Returns new DataFrame reference | Yes | Renaming or clearing (None) 1D or MultiIndex names safely inside pipelines. |
df.index.name = "new_name" |
No (use df.index.names = [...] for MultiIndex) |
Mutates Index metadata in place |
No (None-returning assignment) |
Quick interactive scripts where df owns a standalone index. |
df.reset_index(names="col_name") |
Yes (pass list of names for MultiIndex) | Returns flat DataFrame with new column | Yes | Converting an unnamed index directly into a named DataFrame column in one step. |
You must log in or register to comment.
