Name a DataFrame's row index and reset it into a leading column in pandas
Quick Overview
Implement a pandas function that names a DataFrame's unnamed row index and turns it into the first column of a new frame with a fresh RangeIndex. It tests how rename_axis and reset_index work together, column ordering, and edge cases such as an existing column with the same name.
Name a DataFrame's row index and reset it into a leading column in pandas
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Implement `reset_named_index(df: pd.DataFrame, index_name: str) -> pd.DataFrame`. Name the unnamed row index with `rename_axis(index_name)`, then call `reset_index()` so it becomes a leading column `index_name`.
The result must meet these requirements:
- Column order: `index_name` first, then the original columns.
- The result has a fresh `RangeIndex`.
- It is graded against `df.rename_axis(index_name).reset_index()` via `assert_frame_equal`.
Example:
```python
df = pd.DataFrame({"a": [10, 20], "b": [30, 40]}) # unnamed index 0,1
reset_named_index(df, "id")
# id a b
# 0 0 10 30
# 1 1 20 40
```
```hint Where the column name comes from
`reset_index()` names the new column after the index's `name` attribute. Check what that attribute is for an unnamed index, and think about which order the two calls have to run in.
```
### What a Strong Answer Covers
- Why the index must be named before `reset_index()`, and what the promoted column is called otherwise
- The shape of the result: the new column first, the original columns in their order, and a fresh `RangeIndex`
- Returning a new DataFrame without modifying the caller's frame
- Edge cases such as an index that already has a name, an existing column with the same name, or a MultiIndex
### Follow-up Questions
- How would you rename a column axis instead of the row index?
- What happens if `df` already has a column called `index_name`, and how would you handle it?
- How would you get the same result with a single call on pandas 1.5 or later?
- What changes when the row index is a MultiIndex?
Overview: Implement a pandas function that names a DataFrame's unnamed row index and turns it into the first column of a new frame with a fresh RangeIndex. It tests how rename_axis and reset_index work together, column ordering, and edge cases such as an existing column with the same name.
Implement reset_named_index(df: pd.DataFrame, index_name: str) -> pd.DataFrame. Name the unnamed row index with rename_axis(index_name), then call reset_index() so it becomes a leading column index_name.
The result must meet these requirements:
Column order:
index_name
first, then the original columns.
The result has a fresh
RangeIndex
.
It is graded against
df.rename_axis(index_name).reset_index()
via
assert_frame_equal
.
Example:
df = pd.DataFrame({"a": [10, 20], "b": [30, 40]}) # unnamed index 0,1
reset_named_index(df, "id")
# id a b
# 0 0 10 30
# 1 1 20 40
What a Strong Answer Covers Guidance
Why the index must be named before
reset_index()
, and what the promoted column is called otherwise
The shape of the result: the new column first, the original columns in their order, and a fresh
RangeIndex
Returning a new DataFrame without modifying the caller's frame
Edge cases such as an index that already has a name, an existing column with the same name, or a MultiIndex
Follow-up Questions Guidance
How would you rename a column axis instead of the row index?
What happens if
df
already has a column called
index_name
, and how would you handle it?
How would you get the same result with a single call on pandas 1.5 or later?