Resetting the Index
set_index makes one column the row labels. reset_index moves those labels back into a column.
Make order_id the index
set_index turns the order_id column into row labels. The column leaves the table body.
import pandas as pd
df = pd.DataFrame({
"order_id": [101, 102, 103],
"product": ["Tea", "Coffee", "Mug"],
"units": [2, 1, 3],
})
print(df.set_index("order_id"))Move the labels back
reset_index turns the labels back into a column. The labels sit in the table body again.
import pandas as pd
df = pd.DataFrame({
"order_id": [101, 102, 103],
"product": ["Tea", "Coffee", "Mug"],
"units": [2, 1, 3],
})
indexed = df.set_index("order_id")
print(indexed.reset_index())Clean labels after filtering
Filtering keeps the old labels, so they read 0 and 2. reset_index with drop=True numbers the rows fresh.
import pandas as pd
df = pd.DataFrame({
"product": ["Tea", "Coffee", "Mug"],
"units": [2, 1, 3],
})
big = df[df["units"] > 1]
print(big)
print(big.reset_index(drop=True))Example
import io
import pandas as pd
SALES_CSV = """order_id,date,product,category,units,price,region
101,2026-01-05,Tea,Drink,2,4.50,North
102,2026-01-06,Coffee,Drink,1,6.00,South
103,2026-01-07,Mug,Home,3,12.00,East
104,2026-01-08,Notebook,Office,2,8.50,West
105,2026-01-09,Pen,Office,5,2.00,North
106,2026-01-12,Lamp,Home,1,24.00,South
107,2026-01-13,Tea,Drink,4,4.50,East
108,2026-01-14,Coffee,Drink,2,6.00,West
109,2026-01-15,Mug,Home,1,12.00,North
110,2026-01-16,Notebook,Office,3,8.50,South
111,2026-01-19,Pen,Office,2,2.00,East
112,2026-01-20,Lamp,Home,2,24.00,West"""
sales = pd.read_csv(io.StringIO(SALES_CSV))
north = sales[sales["region"] == "North"]
print(north)
clean = north.reset_index(drop=True)
print(clean)Key points
- Index: the column of row labels on the left of a table.
- Use set_index to make a column the index. The column leaves the table body.
- Use reset_index to turn the labels back into a normal column.
- Pass drop=True to reset_index to throw the old labels away.
- Filtering leaves gaps in the labels. A fresh reset_index gives clean numbers from 0.
In this example, the sales table loads from the CSV text. The filter keeps only North rows, and their labels read 0, 4, and 8. reset_index with drop=True throws the old labels away and numbers the rows 0, 1, and 2.
Note
set_index and reset_index return a new DataFrame. The first frame keeps its old shape.
Try it Yourself
Edit the code, then press Run.
Running is simulated · Python Playground
Exercise
After filtering, the index reads 0, 4, 8. Which call gives clean labels 0, 1, 2?