Pandas Series: reset_index() function
Generate a new Pandas series with the index reset
The reset_index() function is used to generate a new DataFrame or Series with the index reset.
This is useful when the index needs to be treated as a column, or when the index is meaningless and needs to be reset to the default before another operation
Syntax:
Series.reset_index(self, level=None, drop=False, name=None, inplace=False)
Parameters:
Name | Description | Type/Default Value | Required / Optional |
---|---|---|---|
level | For a Series with a MultiIndex, only remove the specified levels from the index. Removes all levels by default. | int, str, tuple, or list, | optional |
drop | Just reset the index, without inserting it as a column in the new DataFrame. | bool Default Value: False |
Required |
name | The name to use for the column containing the original Series values. Uses self.name by default. This argument is ignored when drop is True. | object | optional |
inplace | Modify the Series in place (do not create a new object). | bool Default Value: False |
Required |
Returns:Series or DataFrame
When drop is False (the default), a DataFrame is returned. The newly created columns will come first in the DataFrame, followed by the original Series values. When drop is True, a Series is returned. In either case, if inplace=True, no value is returned.
Example - Generate a DataFrame with default index:
Python-Pandas Code:
import numpy as np
import pandas as pd
s = pd.Series([2, 3, 4, 5], name='f1',
index=pd.Index(['p', 'q', 'r', 's'], name='idx'))
s.reset_index()
Output:
idx f1 0 p 2 1 q 3 2 r 4 3 s 5
Example - To specify the name of the new column use name:
Python-Pandas Code:
import numpy as np
import pandas as pd
s = pd.Series([2, 3, 4, 5], name='f1',
index=pd.Index(['p', 'q', 'r', 's'], name='idx'))
s.reset_index(name='values')
Output:
idx values 0 p 2 1 q 3 2 r 4 3 s 5
Example - To generate a new Series with the default set drop to True:
Python-Pandas Code:
import numpy as np
import pandas as pd
s = pd.Series([2, 3, 4, 5], name='f1',
index=pd.Index(['p', 'q', 'r', 's'], name='idx'))
s.reset_index(drop=True)
Output:
0 2 1 3 2 4 3 5 Name: f1, dtype: int64
Example - To update the Series in place, without generating a new one set inplace to True. Note that it also requires drop=True:
Python-Pandas Code:
import numpy as np
import pandas as pd
s = pd.Series([2, 3, 4, 5], name='f1',
index=pd.Index(['p', 'q', 'r', 's'], name='idx'))
s.reset_index(inplace=True, drop=True)
s
Output:
0 2 1 3 2 4 3 5 Name: f1, dtype: int64
The level parameter is interesting for Series with a multi-level index.
Example - To remove a specific level from the Index, use level:
Python-Pandas Code:
import numpy as np
import pandas as pd
arrays = [np.array(['b1', 'b2', 's1', 's2']),
np.array(['one', 'two', 'one', 'two'])]
s2 = pd.Series(
range(4), name='f1',
index=pd.MultiIndex.from_arrays(arrays,
names=['p', 'q']))
s2.reset_index(level='p')
Output:
p f1 q one b1 0 two b2 1 one s1 2 two s2 3
Example - If level is not set, all levels are removed from the Index:
Python-Pandas Code:
import numpy as np
import pandas as pd
arrays = [np.array(['b1', 'b2', 's1', 's2']),
np.array(['one', 'two', 'one', 'two'])]
s2 = pd.Series(
range(4), name='f1',
index=pd.MultiIndex.from_arrays(arrays,
names=['p', 'q']))
s2.reset_index()
Output:
p q f1 0 b1 one 0 1 b2 two 1 2 s1 one 2 3 s2 two 3
Previous: Set the name of the axis in Pandas
Next: Random items from an axis of Pandas object
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics