- API reference
- Series
- pandas.Serie...
pandas.Series.reset_index#
- Series.reset_index(level=None,*,drop=False,name=<no_default>,inplace=False,allow_duplicates=False)[source]#
Generate a new DataFrame or Series with the index reset.
This is useful when the index needs to be treated as a column, orwhen the index is meaningless and needs to be reset to the defaultbefore another operation.
- Parameters:
- levelint, str, tuple, or list, default optional
For a Series with a MultiIndex, only remove the specified levelsfrom the index. Removes all levels by default.
- dropbool, default False
Just reset the index, without inserting it as a column inthe new DataFrame.
- nameobject, optional
The name to use for the column containing the original Seriesvalues. Uses
self.nameby default. This argument is ignoredwhendrop is True.- inplacebool, default False
Modify the Series in place (do not create a new object).
- allow_duplicatesbool, default False
Allow duplicate column labels to be created.
Added in version 1.5.0.
- Returns:
- Series or DataFrame or None
Whendrop is False (the default), a DataFrame is returned.The newly created columns will come first in the DataFrame,followed by the original Series values.Whendrop is True, aSeries is returned.In either case, if
inplace=True, no value is returned.
See also
DataFrame.reset_indexAnalogous function for DataFrame.
Examples
>>>s=pd.Series([1,2,3,4],name='foo',...index=pd.Index(['a','b','c','d'],name='idx'))
Generate a DataFrame with default index.
>>>s.reset_index() idx foo0 a 11 b 22 c 33 d 4
To specify the name of the new column usename.
>>>s.reset_index(name='values') idx values0 a 11 b 22 c 33 d 4
To generate a new Series with the default setdrop to True.
>>>s.reset_index(drop=True)0 11 22 33 4Name: foo, dtype: int64
Thelevel parameter is interesting for Series with a multi-levelindex.
>>>arrays=[np.array(['bar','bar','baz','baz']),...np.array(['one','two','one','two'])]>>>s2=pd.Series(...range(4),name='foo',...index=pd.MultiIndex.from_arrays(arrays,...names=['a','b']))
To remove a specific level from the Index, uselevel.
>>>s2.reset_index(level='a') a foobone bar 0two bar 1one baz 2two baz 3
Iflevel is not set, all levels are removed from the Index.
>>>s2.reset_index() a b foo0 bar one 01 bar two 12 baz one 23 baz two 3