set_index and reset_index
df.set_index("col") makes that column the row index (so loc can look rows up by its values) and df.reset_index() moves the index back into a regular column, giving the frame a fresh 0..n-1 index. reset_index(drop=True) discards the old index instead of keeping it.
Syntax
df.set_index(keys, drop=True, append=False) | df.reset_index(level=None, drop=False, names=None)| Part | What it is |
|---|---|
keys | Column name or list of names to become the index |
drop | set_index: remove the column from the data (default True); reset_index: discard the index instead of inserting it |
names | reset_index: name(s) for the new column(s) |
Examples
import pandas as pd
df = pd.DataFrame({"code": ["NL", "BE", "FR"], "pop": [17.9, 11.7, 68.2]})
c = df.set_index("code")
print(c.loc["BE", "pop"])Result: 11.7
c.reset_index()Result: code pop 0 NL 17.9 1 BE 11.7 2 FR 68.2
f = df[df["pop"] > 15]
f.reset_index(drop=True)Result: code pop 0 NL 17.9 1 FR 68.2
After filtering, drop=True renumbers the rows without adding an index column.
Common mistakes
- A column named index appears after reset_index: pass drop=True when you do not need the old index.
- KeyError after set_index: the column is no longer in df.columns; access it through df.index or use drop=False.
- ValueError: cannot insert level_0, already exists: you reset twice; drop the leftover column or use drop=True.
Practice set_index and reset_index
These drills check your answer on live data, free, no account needed.
- Group means as a flat tableReturn the average `units` per product as a DataFrame with two columns, `product` and `units`, and a plain 0, 1, 2 index.
- Index level into columns with unstack`s` holds revenue indexed by region and quarter. Use `unstack` so regions are rows and quarters are columns. Missing combinations stay NaN.
Questions
When should I set an index?
When you look rows up by a key (loc), align several frames on that key, or work with time series (resample needs a DatetimeIndex). For plain tabular work a default index is fine.
How do I set multiple columns as the index?
df.set_index(["year", "region"]) creates a MultiIndex; reset_index() brings both back as columns.
How do I rename the index?
df.index.name = "id" or df.rename_axis("id").
Related
- loc vs ilocThe difference between loc and iloc in pandas: loc selects by index label and column name, iloc by integer position. Slices, lists and boolean masks.
- groupbyHow groupby works in pandas: group by one or more columns, apply sum, mean, count or size, get a flat table with as_index=False, and iterate over groups.
- pivotHow df.pivot works in pandas: turn a long table with key and value columns into a wide table, what index, columns and values mean, and the duplicates error.
- sort_valuesHow to sort a pandas DataFrame with sort_values: ascending and descending, several columns with mixed order, NaN first, sort by index and reset the index.