Flatten indexed data
Convert index labels into ordinary tabular data for export, joins, or downstream transformations.
Pandas · Module
Resets a DataFrame's index to the default integer index. By default, the former index is retained as column data, but it can instead be discarded or selectively reset for a MultiIndex.
Call reset_index to replace the current row index with the default integer index. Unless drop=True, the old index is retained as a column; level can limit removal for a MultiIndex, while inplace=True changes the existing DataFrame and returns None.
DataFrame.reset_index(*level=None*, ***, *drop=False*, *inplace=False*, *col_level=0*, *col_fill='',* *allow_duplicates=<no_default>*, *names=None*)| Name | Type | Description |
|---|---|---|
level | int, str, tuple, or list, default None | Index level(s) to remove; accepts an integer, name, tuple, or list. Defaults to all levels. |
drop | bool, default False | If true, discard the old index instead of adding it as a column. |
inplace | bool, default False | If true, modify this DataFrame and return None; otherwise create a result. |
col_level | int or str, default 0 | Column level into which index labels are inserted when columns are hierarchical. |
names | int, str, or 1-dimensional list, default None | Label used for the inserted index column(s), or a list of labels for a MultiIndex. |
DataFrame or None: a DataFrame with the new index, or None when inplace=True.
level to target particular index levels instead of resetting every level.level as None resets the complete index.drop=True when the former index should not become a data column.inplace controls whether the original object is changed or a separate result is produced.col_level chooses where inserted index labels are placed.drop=True, the old index is discarded rather than added as a column.names, provide one name per level.import pandas as pd
df = pd.DataFrame(
{"city": ["Paris", "Tokyo"], "sales": [10, 20]},
index=[101, 102],
)
result = df.reset_index(names="customer_id")
print(result)Creates a new DataFrame with the original index in a renamed column and a default integer index.
Convert index labels into ordinary tabular data for export, joins, or downstream transformations.
Remove only selected levels while preserving the remaining MultiIndex structure.
Assign a meaningful column name while moving index values into the columns.
DataFrame.reset_index() do by default?It replaces the DataFrame's index with the default integer index. By default, the previous index values become a column; drop=True prevents that insertion.
Follow-up: How would you reset only one level of a MultiIndex? Pass that level's integer position or name through level, leaving other levels intact.
level selects which index level or levels to remove, and None removes all levels. It accepts an integer, string, tuple, or list.
Follow-up: What should level contain for several named levels? Use a list or tuple containing those level names.
inplace change?With inplace=False, the method returns a new DataFrame. With inplace=True, it modifies the existing object and returns None.
Follow-up: When is inplace=False preferable? It preserves the original object and makes the transformed result available for assignment or chaining.
DataFrame.set_indexDataFrame.reindexSeries.reset_index