pandas 1.4.2

ParametersReturnsBackRef
reset_index(self, level: 'Hashable | Sequence[Hashable] | None' = None, drop: 'bool' = False, inplace: 'bool' = False, col_level: 'Hashable' = 0, col_fill: 'Hashable' = '') -> 'DataFrame | None'

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

Modify the DataFrame in place (do not create a new object).

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.

Returns

DataFrame or None

DataFrame with the new index or None if inplace=True .

Reset the index, or a level of it.

See Also

DataFrame.reindex

Change to new indices or expand indices.

DataFrame.reindex_like

Change to same indices as other DataFrame.

DataFrame.set_index

Opposite of reset_index.

Examples

This example is valid syntax, but we were not able to check execution
>>> df = pd.DataFrame([('bird', 389.0),
...  ('bird', 24.0),
...  ('mammal', 80.5),
...  ('mammal', np.nan)],
...  index=['falcon', 'parrot', 'lion', 'monkey'],
...  columns=('class', 'max_speed'))
... df class max_speed falcon bird 389.0 parrot bird 24.0 lion mammal 80.5 monkey mammal NaN

When we reset the index, the old index is added as a column, and a new sequential index is used:

This example is valid syntax, but we were not able to check execution
>>> df.reset_index()
    index   class  max_speed
0  falcon    bird      389.0
1  parrot    bird       24.0
2    lion  mammal       80.5
3  monkey  mammal        NaN

We can use the drop parameter to avoid the old index being added as a column:

This example is valid syntax, but we were not able to check execution
>>> df.reset_index(drop=True)
    class  max_speed
0    bird      389.0
1    bird       24.0
2  mammal       80.5
3  mammal        NaN

You can also use reset_index with MultiIndex .

This example is valid syntax, but we were not able to check execution
>>> index = pd.MultiIndex.from_tuples([('bird', 'falcon'),
...  ('bird', 'parrot'),
...  ('mammal', 'lion'),
...  ('mammal', 'monkey')],
...  names=['class', 'name'])
... columns = pd.MultiIndex.from_tuples([('speed', 'max'),
...  ('species', 'type')])
... df = pd.DataFrame([(389.0, 'fly'),
...  ( 24.0, 'fly'),
...  ( 80.5, 'run'),
...  (np.nan, 'jump')],
...  index=index,
...  columns=columns)
... df speed species max type class name bird falcon 389.0 fly parrot 24.0 fly mammal lion 80.5 run monkey NaN jump

If the index has multiple levels, we can reset a subset of them:

This example is valid syntax, but we were not able to check execution
>>> df.reset_index(level='class')
         class  speed species
                  max    type
name
falcon    bird  389.0     fly
parrot    bird   24.0     fly
lion    mammal   80.5     run
monkey  mammal    NaN    jump

If we are not dropping the index, by default, it is placed in the top level. We can place it in another level:

This example is valid syntax, but we were not able to check execution
>>> df.reset_index(level='class', col_level=1)
                speed species
         class    max    type
name
falcon    bird  389.0     fly
parrot    bird   24.0     fly
lion    mammal   80.5     run
monkey  mammal    NaN    jump

When the index is inserted under another level, we can specify under which one with the parameter :None:None:`col_fill`:

This example is valid syntax, but we were not able to check execution
>>> df.reset_index(level='class', col_level=1, col_fill='species')
              species  speed species
                class    max    type
name
falcon           bird  389.0     fly
parrot           bird   24.0     fly
lion           mammal   80.5     run
monkey         mammal    NaN    jump

If we specify a nonexistent level for :None:None:`col_fill`, it is created:

This example is valid syntax, but we were not able to check execution
>>> df.reset_index(level='class', col_level=1, col_fill='genus')
                genus  speed species
                class    max    type
name
falcon           bird  389.0     fly
parrot           bird   24.0     fly
lion           mammal   80.5     run
monkey         mammal    NaN    jump
See :

Back References

The following pages refer to to this document either explicitly or contain code examples using this.

pandas.core.generic.NDFrame.reindex pandas.core.series.Series.reset_index pandas.core.frame.DataFrame.reset_index pandas.core.frame.DataFrame.reindex pandas.core.frame.DataFrame.set_index pandas.core.series.Series.reindex pandas.core.generic.NDFrame.reindex_like

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


File: /pandas/core/frame.py#5632
type: <class 'function'>
Commit: