设置与重置索引
使用 set_index() 将列提升为行索引,使用 reset_index() 将其恢复为列,并了解 MultiIndex。
设置与重置索引 是 CoddyKit 上的免费 Pandas & NumPy Academy 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Pandas & NumPy Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Pandas & NumPy Academy 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
What Is the DataFrame Index?
Every Pandas DataFrame has a row index — a set of labels attached to each row. The default index is a RangeIndex (0, 1, 2, …), but you can replace it with any column that acts as a natural identifier: a date, a product ID, a user ID, or any unique key. A meaningful index improves readability, enables label-based slicing, and is required for time series resampling.
import pandas as pd
df = pd.DataFrame({
'date': ['2024-01-01', '2024-01-02', '2024-01-03'],
'sales': [100, 200, 150]
})
print('Default index:', df.index.tolist()) # [0, 1, 2]
print(df)set_index() — Promoting a Column
DataFrame.set_index('col') promotes the specified column to become the row index, removing it from the regular columns. The column's values become row labels. This is the standard way to make a DataFrame more expressive when one column acts as a natural key. A new DataFrame is returned; the original is unchanged unless inplace=True is used.
import pandas as pd
df = pd.DataFrame({
'date': ['2024-01-01', '2024-01-02', '2024-01-03'],
'sales': [100, 200, 150]
})
df = df.set_index('date')
print(df)
# sales
# date
# 2024-01-01 100
# 2024-01-02 200
# 2024-01-03 150
print(df.index) # Index(['2024-01-01', ...], dtype='object', name='date')Selecting Rows with a Custom Index
Once a meaningful column is the index, you can use .loc[label] to retrieve rows by label with clean, readable syntax — no boolean masks required. For a DatetimeIndex, you can even use partial date strings like df.loc['2024-01'] to select all rows in January 2024.
import pandas as pd
df = pd.DataFrame({
'product': ['A', 'B', 'C'],
'price': [10, 20, 30],
'stock': [100, 50, 75]
})
df = df.set_index('product')
# Access row by label
print(df.loc['B'])
# price 20
# stock 50
# Name: B, dtype: int64set_index() with Multiple Columns — MultiIndex
Passing a list of column names to set_index() creates a MultiIndex (hierarchical index). The resulting DataFrame can be accessed using tuples in .loc[]. MultiIndex is essential for grouped time series (region + date), panel data (subject + time), and any analysis requiring two-level row grouping.
import pandas as pd
df = pd.DataFrame({
'region': ['East', 'East', 'West', 'West'],
'year': [2023, 2024, 2023, 2024],
'revenue': [100, 150, 200, 220]
})
df = df.set_index(['region', 'year'])
print(df)
# revenue
# region year
# East 2023 100
# 2024 150
# West 2023 200
# 2024 220
print(df.loc[('East', 2024)]) # revenue = 150drop= Parameter in set_index()
By default, set_index('col') removes the column from the DataFrame's regular columns once it becomes the index. Pass drop=False to keep the column in place while also using it as the index. This is sometimes useful when you want label-based access but also need the column available for computations in the regular column space.
import pandas as pd
df = pd.DataFrame({'id': [1, 2, 3], 'value': [10, 20, 30]})
# Keep 'id' as both index and column
df_with_both = df.set_index('id', drop=False)
print(df_with_both)
# id value
# id
# 1 1 10
# 2 2 20
# 3 3 30reset_index() — Moving Index Back to Column
reset_index() is the inverse of set_index(): it moves the current row index back into a regular column and replaces the index with the default RangeIndex. This is commonly needed after a groupby().agg() or after operations that leave you with a meaningful index you need to use as a column in further processing.
import pandas as pd
df = pd.DataFrame({'sales': [100, 200]}, index=['East', 'West'])
df.index.name = 'region'
reset = df.reset_index()
print(reset)
# region sales
# 0 East 100
# 1 West 200
print(reset.index.tolist()) # [0, 1] — default RangeIndex restoredreset_index(drop=True)
Passing drop=True to reset_index() discards the current index entirely instead of moving it to a column. Use this when the current index holds no meaningful information (e.g., after filtering rows, the index has gaps like 0, 3, 7) and you just want a clean sequential index starting from 0.
import pandas as pd
df = pd.DataFrame({'x': [10, 20, 30, 40, 50]})
filtered = df[df['x'] > 15]
print('Filtered index:', filtered.index.tolist()) # [1, 2, 3, 4]
# Drop the old index — don't move it to a column
clean = filtered.reset_index(drop=True)
print('Clean index:', clean.index.tolist()) # [0, 1, 2, 3]reset_index() After groupby
After calling groupby().agg(), the groupby keys become the index. Calling reset_index() converts them back to regular columns, giving a flat tabular result that is easier to work with, merge, or export. This is one of the most common uses of reset_index() in practice.
import pandas as pd
df = pd.DataFrame({
'dept': ['Eng', 'HR', 'Eng', 'Sales', 'HR'],
'salary': [90000, 50000, 80000, 70000, 55000]
})
# After groupby, dept becomes the index
agg = df.groupby('dept')['salary'].mean()
print(type(agg), agg.index.tolist())
# dept is the index
# Reset makes dept a regular column again
result = agg.reset_index()
result.columns = ['dept', 'avg_salary']
print(result)rename_axis() for Index Name
When the row index doesn't have a name, or when you want to rename it for clarity, use df.rename_axis('new_name') (for the row index) or df.rename_axis('col_name', axis=1) (for the column axis). Giving the index a meaningful name makes the output more self-documenting, especially when exporting to CSV where the index name becomes a column header.
import pandas as pd
df = pd.DataFrame({'sales': [100, 200, 300]}, index=['A', 'B', 'C'])
print(df.index.name) # None
# Name the index
df = df.rename_axis('region')
print(df)
# sales
# region
# A 100
# B 200
# C 300Reindexing to Fill Gaps
DataFrame.reindex(new_index) reshapes the DataFrame to match a new index, filling positions that exist in the new index but not in the original data with NaN. This is used to align DataFrames to a common complete index — for example, ensuring every month in a year appears even if no data exists for some months.
import pandas as pd
df = pd.DataFrame({
'month': ['Jan', 'Mar', 'Jun'],
'revenue': [100, 200, 300]
}).set_index('month')
all_months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun']
complete = df.reindex(all_months)
print(complete)
# revenue
# Jan 100.0
# Feb NaN <- filled with NaN
# Mar 200.0
# Apr NaN
# May NaN
# Jun 300.0MultiIndex reset_index and Level Selection
For a MultiIndex DataFrame, reset_index() moves all levels back to columns by default. Pass level= to reset only specific levels. This is useful when you want to keep one level as the index (e.g., keep the date as the index) and move only the other level to a column.
import pandas as pd
df = pd.DataFrame(
{'revenue': [100, 150, 200, 220]},
index=pd.MultiIndex.from_tuples(
[('East', 2023), ('East', 2024), ('West', 2023), ('West', 2024)],
names=['region', 'year']
)
)
# Reset only the 'year' level, keep 'region' as index
df2 = df.reset_index(level='year')
print(df2)
# year revenue
# region
# East 2023 100
# East 2024 150
# West 2023 200
# West 2024 220Quick Check
Test your understanding of setting and resetting the index.
Lesson Recap
In this lesson you learned: set_index('col') promotes a column to the row index for label-based access, passing a list creates a MultiIndex, and reset_index() moves the index back to a column (use drop=True to discard it instead). Use reindex() to align to a complete target index filling missing positions with NaN. These techniques are foundational for the GroupBy and Merging lessons ahead.
常见问题解答
「设置与重置索引」课时是免费的吗?
是的 — 「设置与重置索引」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Pandas & NumPy Academy 课程的其余内容,请升级到 CoddyKit PRO。 Pandas & NumPy Academy 课程共包含 4 节课。
「设置与重置索引」这节课中我会学到什么?
使用 set_index() 将列提升为行索引,使用 reset_index() 将其恢复为列,并了解 MultiIndex。 你通过在浏览器中直接运行的动手代码来练习 Pandas & NumPy Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Pandas & NumPy Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Pandas & NumPy Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「设置与重置索引」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Pandas & NumPy Academy 课中编写并运行代码吗?
能。每节 Pandas & NumPy Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。