How to change index values of a Pandas series after creation?


The pandas series constructor will automatically create series index labels based on the given data. If you want to specify those index labels, we can give those index values separately by using the index keyword argument of the pandas series function.

The Python dictionary is a data to the pandas series and we have not specified any index labels then, the keys of python dictionary values are taken as index labels.

It is possible to specify or change the index labels of a pandas Series object after creation also. It can be done by using the index attribute of the pandas series constructor.

Example

import pandas as pd

# create a series
s = pd.Series([1,2,3,4,6])

print(s)

# change the index
s.index = list('ABCDE')
print('
Series with new index') print(s)

Explanation

pd is pandas package alias name defined while importing the pandas package. After that created a simple pandas Series object by using the pandas Series function, here the data is a list of integers.

And the index labels for our data are not specified but the pandas Series constructor has created an index label for our data automatically that labels are from 0-4 values.

In this example, we have changed index labels by using index attributes from 0, 1, 2, 3, 4 to A, B, C, D, E.

Output

0   1
1   2
2   3
3   4
4   6
dtype: int64

Series with new index
A   1
B   2
C   3
D   4
E   6
dtype: int64

There are two sets in the above output block, one block is the output of our initial series object with auto-generated index labels. And the second block is the output of the series object ‘s’ after changing the index labels.

Here we have changed the index labels of our pandas Series object from integer values to object data type.

Example

This example will tell you another approach to change the index labels of our pandas Series object after creation. Here we have the Series.rename() function in the pandas Series functionalities, which is used to change the series index labels or to change the name of the series object.

import pandas as pd

# create a series
s = pd.Series([1,2,3,4,6])

print(s)

# change the index
s = s.rename(lambda x: x**2)
print('
Series with new labels') print(s)

Explanation

The lambda function is given as a parameter to the Series.rename method, and this python lambda function will generate square values to our index labels.

With these generated square values the Series.rename method will return a new series object as an output, and it won’t update the actual series object ‘s’. That’s why here we have re-assigned the resultant object to the series object ‘s’ again.

Output

0   1
1   2
2   3
3   4
4   6
dtype: int64

Series with new labels
0   1
1   2
4   3
9   4
16  6
dtype: int64

The first block is the output of the initial series object with default index labels, and the second one is the resultant series object with updated index labels.

Updated on: 17-Nov-2021

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements