- API reference
- DataFrame
- pandas.DataF...
pandas.DataFrame.reset_index#
- DataFrame.reset_index(level=None,*,drop=False,inplace=False,col_level=0,col_fill='',allow_duplicates=<no_default>,names=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 morelevels.
- Parameters:
- levelint, str, tuple, or list, default None
Only remove the given levels from the index. Removes all levels bydefault.
- dropbool, default False
Do not try to insert index into dataframe columns. This resetsthe index to the default integer index.
- inplacebool, default False
Whether to modify the DataFrame rather than creating a new one.
- col_levelint or str, default 0
If the columns have multiple levels, determines which level thelabels are inserted into. By default it is inserted into the firstlevel.
- col_fillobject, default ‘’
If the columns have multiple levels, determines how the otherlevels are named. If None then the index name is repeated.
- allow_duplicatesbool, optional, default lib.no_default
Allow duplicate column labels to be created.
Added in version 1.5.0.
- namesint, str or 1-dimensional list, default None
Using the given string, rename the DataFrame column which contains theindex data. If the DataFrame has a MultiIndex, this has to be a list ortuple with length equal to the number of levels.
Added in version 1.5.0.
- Returns:
- DataFrame or None
DataFrame with the new index or None if
inplace=True
.
See also
DataFrame.set_index
Opposite of reset_index.
DataFrame.reindex
Change to new indices or expand indices.
DataFrame.reindex_like
Change to same indices as other DataFrame.
Examples
>>>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_speedfalcon bird 389.0parrot bird 24.0lion mammal 80.5monkey mammal NaN
When we reset the index, the old index is added as a column, and anew sequential index is used:
>>>df.reset_index() index class max_speed0 falcon bird 389.01 parrot bird 24.02 lion mammal 80.53 monkey mammal NaN
We can use thedrop parameter to avoid the old index being added asa column:
>>>df.reset_index(drop=True) class max_speed0 bird 389.01 bird 24.02 mammal 80.53 mammal NaN
You can also usereset_index withMultiIndex.
>>>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 typeclass namebird falcon 389.0 fly parrot 24.0 flymammal lion 80.5 run monkey NaN jump
Using thenames parameter, choose a name for the index column:
>>>df.reset_index(names=['classes','names']) classes names speed species max type0 bird falcon 389.0 fly1 bird parrot 24.0 fly2 mammal lion 80.5 run3 mammal monkey NaN jump
If the index has multiple levels, we can reset a subset of them:
>>>df.reset_index(level='class') class speed species max typenamefalcon bird 389.0 flyparrot bird 24.0 flylion mammal 80.5 runmonkey mammal NaN jump
If we are not dropping the index, by default, it is placed in the toplevel. We can place it in another level:
>>>df.reset_index(level='class',col_level=1) speed species class max typenamefalcon bird 389.0 flyparrot bird 24.0 flylion mammal 80.5 runmonkey mammal NaN jump
When the index is inserted under another level, we can specify underwhich one with the parametercol_fill:
>>>df.reset_index(level='class',col_level=1,col_fill='species') species speed species class max typenamefalcon bird 389.0 flyparrot bird 24.0 flylion mammal 80.5 runmonkey mammal NaN jump
If we specify a nonexistent level forcol_fill, it is created:
>>>df.reset_index(level='class',col_level=1,col_fill='genus') genus speed species class max typenamefalcon bird 389.0 flyparrot bird 24.0 flylion mammal 80.5 runmonkey mammal NaN jump