Pandas series objects are used in python for handling sequential data. For handling data in a series, we generally use the indices of the elements. In this article, we will discuss how to rest index in a pandas series.
How to Reset the Index in a Pandas Series?
To reset the index in a pandas series, we can use two approaches. First, we can assign a list containing new indices directly to the index
attribute of the series object. Alternatively, we can use the reset_index()
method. Let us discuss both these approaches.
Reset the Index in a Series Using the Index Attribute
The index
attribute stores the index of a pandas series. To reset the index of a series object, we will first find the length of the series object using the len()
function. The len()
function takes an iterable object as its input argument and returns the length.
After finding the length of the series, we will create a range object containing numbers from 0 to the length of the series using the range()
function. The range()
function takes the highest values in the range as its input argument and returns a range object from 0 to (highest value-1). Finally, we will assign the range object to the index
attribute of the series. After the execution of the above statements, we will get a series with new indices. You can observe this in the following example.
import pandas as pd
import numpy as np
letters=["a","b","c","ab","abc","abcd","bc","d"]
numbers=[3,23,11,14,16,2,45,65]
series=pd.Series(letters)
series.index=numbers
print("The original series is:")
print(series)
lenSeries=len(series)
indices=range(lenSeries)
series.index=indices
print("The modified series is:")
print(series)
Output:
The original series is:
3 a
23 b
11 c
14 ab
16 abc
2 abcd
45 bc
65 d
dtype: object
The modified series is:
0 a
1 b
2 c
3 ab
4 abc
5 abcd
6 bc
7 d
dtype: object
In the above example, you can observe that the indices in the original dataframe have been removed and new indices from 0 to 7 have been assigned to the elements.
In the above approach, you need to make sure that the python list being assigned to the index attribute of the series must have a length equal to the number of elements in the series. Otherwise, the program will run into a ValueError exception.
Instead of using the index
attribute, we can use the reset_index()
method to reset the index of a pandas series.
Suggested Reading: If you are into machine learning, you can read this MLFlow tutorial with code examples. You might also like this article on clustering mixed data types in Python.
The reset_index() Method
The syntax of the reset_index()
method is as follows.
Series.reset_index(level=None, *, drop=False, name=_NoDefault.no_default, inplace=False, allow_duplicates=False)
Here,
- The
level
parameter is used to select the level of index that needs to be removed in case of multilevel indices. You can pass the level, list of levels, index name, or the list of index names of the index that needs to be dropped from the series to thelevel
parameter. - By default, when we reset the index of a series using the
reset_index()
method, the index is added as an extra column and we get a dataframe as output from thereset_index()
method instead of a series. If you want to remove the index instead of converting it to a column, you can set thedrop
parameter to True. - If you want to use the index from the original series as a new column in the output of the
reset_index()
method, you can use the name parameter to set the name of the column containing the data values. The name of the column containing index values is set to“index”
by default. In cases when thedrop
parameter is set to True, thename
parameter will be ignored. - By default, the
reset_index()
method returns a new Series after resetting the index. To modify the original series, you can set theinplace
parameter toTrue
. - The
allow_duplicates
parameter is used to decide if duplicate column labels are allowed in the Series or not. To reset the index of a Series, theallow_duplicates
parameter has no use.
Reset the Index in a Series Using the reset_index() Method
To reset the index in a series, you can simply invoke the reset_index()
method on the series object as shown below.
import pandas as pd
import numpy as np
letters=["a","b","c","ab","abc","abcd","bc","d"]
numbers=[3,23,11,14,16,2,45,65]
series=pd.Series(letters)
series.index=numbers
print("The original series is:")
print(series)
series=series.reset_index()
print("The modified series is:")
print(series)
Output:
The original series is:
3 a
23 b
11 c
14 ab
16 abc
2 abcd
45 bc
65 d
dtype: object
The modified series is:
index 0
0 3 a
1 23 b
2 11 c
3 14 ab
4 16 abc
5 2 abcd
6 45 bc
7 65 d
You can observe that the reset_index()
method returns a dataframe. The reset_index()
method promotes the current index into a column and returns a pandas dataframe instead of a series.
Here, the name of the column containing the data values is set to 0. You can set the name of the data column using the name parameter in the reset_index()
method as shown in the following example.
import pandas as pd
import numpy as np
letters=["a","b","c","ab","abc","abcd","bc","d"]
numbers=[3,23,11,14,16,2,45,65]
series=pd.Series(letters)
series.index=numbers
print("The original series is:")
print(series)
series=series.reset_index(name="letters")
print("The modified series is:")
print(series)
Output:
The original series is:
3 a
23 b
11 c
14 ab
16 abc
2 abcd
45 bc
65 d
dtype: object
The modified series is:
index letters
0 3 a
1 23 b
2 11 c
3 14 ab
4 16 abc
5 2 abcd
6 45 bc
7 65 d
In the above example, we have passed the literal "letters"
to the name parameter in the reset_index()
method. Hence, when the reset_index()
method is executed, it returns a dataframe with two columns namely index and letters. Here, "index"
is the name of the column which has been created from the original index of the series. Whereas, "letters"
is the name of the column containing the data in the series.
If you don’t want to create a dataframe and drop the index column while resetting the index, you can set the drop
parameter in the reset_index()
method to True as shown below.
import pandas as pd
import numpy as np
letters=["a","b","c","ab","abc","abcd","bc","d"]
numbers=[3,23,11,14,16,2,45,65]
series=pd.Series(letters)
series.index=numbers
print("The original series is:")
print(series)
series=series.reset_index(drop=True)
print("The modified series is:")
print(series)
Output:
The original series is:
3 a
23 b
11 c
14 ab
16 abc
2 abcd
45 bc
65 d
dtype: object
The modified series is:
0 a
1 b
2 c
3 ab
4 abc
5 abcd
6 bc
7 d
dtype: object
In the above example, we have set the drop
parameter to True. Hence, the reset_index()
method returns a series instead of a dataframe.
Reset Index Inplace Using the reset_index() Method
By default, the rest_index()
method returns a new Series. If you want to reset the index in the original series, you can set the inplace
parameter to True as shown below.
import pandas as pd
import numpy as np
letters=["a","b","c","ab","abc","abcd","bc","d"]
numbers=[3,23,11,14,16,2,45,65]
series=pd.Series(letters)
series.index=numbers
print("The original series is:")
print(series)
series.reset_index(drop=True,inplace=True)
print("The modified series is:")
print(series)
Output:
The original series is:
3 a
23 b
11 c
14 ab
16 abc
2 abcd
45 bc
65 d
dtype: object
The modified series is:
0 a
1 b
2 c
3 ab
4 abc
5 abcd
6 bc
7 d
dtype: object
In this example, we have set the inplace
parameter to True in the reset_index()
method. Hence, the indices are dropped from the original series instead of creating a new series. In this case, the reset_index()
method returns None after execution.
Conclusion
In this article, we have discussed different ways to reset index in a pandas series. To know more about pandas module, you can read this article on how to sort a pandas dataframe. You might also like this article on how to drop columns from a pandas dataframe.
I hope you enjoyed reading this article. Stay tuned for more informative articles.
Happy Learning!
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.