modin.pandas.DataFrame.reset_index¶
- DataFrame.reset_index(level: IndexLabel = None, *, drop: bool = False, inplace: bool = False, col_level: Hashable = 0, col_fill: Hashable = '', allow_duplicates=_NoDefault.no_default, names: Hashable | Sequence[Hashable] = None) DataFrame | Series | None[source]¶
Reset the index, or a level of it.
Reset the index of the DataFrame, and use the default one instead. If the DataFrame has a MultiIndex, this method can remove one or more levels.
- Parameters:
level (int, str, tuple, or list, default None) – Only remove the given levels from the index. Removes all levels by default.
drop (bool, default False) – Do not try to insert index into dataframe columns. This resets the index to the default integer index.
inplace (bool, default False) – Whether to modify the DataFrame rather than creating a new one.
col_level (int or str, default 0) – If the columns have multiple levels, determines which level the labels are inserted into. By default, it is inserted into the first level.
col_fill (object, default '') – If the columns have multiple levels, determines how the other levels are named. If None then the index name is repeated.
allow_duplicates (bool, optional, default lib.no_default) – Allow duplicate column labels to be created.
names (int, str or 1-dimensional list, default None) – Using the given string, rename the DataFrame column which contains the index data. If the DataFrame has a MultiIndex, this has to be a list or tuple with length equal to the number of levels.
- Returns:
DataFrame with the new index or None if
inplace=True.- Return type:
DataFrame or None
See also
Series.reset_indexAnalogous function for Series.
DataFrame.set_indexOpposite of reset_index.
DataFrame.reindexChange to new indices or expand indices.
DataFrame.reindex_likeChange to same indices as other DataFrame.
Examples
When we reset the index, the old index is added as a column, and a new sequential index is used:
We can use the drop parameter to avoid the old index being added as a column:
You can also use reset_index with MultiIndex.
Using the names parameter, choose a name for the index column:
If the index has multiple levels, we can reset a subset of them:
If we are not dropping the index, by default, it is placed in the top level. We can place it in another level:
When the index is inserted under another level, we can specify under which one with the parameter col_fill:
If we specify a nonexistent level for col_fill, it is created: