- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
- Related Articles
- How to get the index and values of series in Pandas?
- Java Program to change JLabel text after creation
- Python Pandas - Return a Series containing counts of unique values from Index object
- How to suffix a string to pandas series index labels?
- Python - How to reset index after Groupby pandas?
- How to specify an index while creating a Series in Pandas?
- Python Pandas - Return a Series containing counts of unique values from Index object considering NaN values as well
- Python Pandas - Return a Series containing counts of unique values from Index object sorted in Ascending Order
- How to replace NaN values by Zeroes in a column of a Pandas Series?
- Python Pandas - Return a list of the Index values
- How does pandas series argsort handles nan values?
- Python Pandas - Create a Series from TimeDeltaIndex and set the index of the resulting Series
- How to change the row index after sampling an R data frame?
- Python Pandas - Indicate duplicate index values
- How to retrieve the last valid index from a series object using pandas series.last_valid_index() method?
