• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Basics / Rename Index in a Pandas Series

Rename Index in a Pandas Series

Author: Aditya Raj
Last Updated: December 2, 2022

We use pandas series objects for various data processing tasks in python. In this article, we will discuss how to rename the index in a pandas series.

Table of Contents
  1. Rename Index in a Pandas Series Using the index Attribute
  2. Rename the Index of a Series Using the rename_axis() Method
  3. Rename Index in a Series Inplace in Python
  4. Conclusion

Rename Index in a Pandas Series Using the index Attribute

When a series is created, the name of the index is empty. To rename the index of the series, you can use the name attribute of the series index object. You can assign the new index name to the name attribute of the index object to rename the series index 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,index=numbers)
series.index.name="Numbers"
print("The series is:")
print(series)

Output:

The series is:
Numbers
3        a
23       b
11       c
14      ab
16     abc
2     abcd
45      bc
65       d
dtype: object

In the above example, We have first created a pandas series using the Series() constructor. Then, we assigned the string "Numbers" to the index.name attribute of the pandas series. Hence, the series index is renamed to "Numbers".

To rename the index of a series, you can also use the rename_axis() method. 

Rename the Index of a Series Using the rename_axis() Method

The rename_axis() method has the following syntax.

Series.rename_axis(mapper=_NoDefault.no_default, *, inplace=False, **kwargs)

Here, 

  • The mapper parameter takes the new name of the index as its input argument. 
  • By default, the rename_axis() method returns a new series object. To modify the original series on which the rename_axis() method is invoked, you can set the inplace parameter to True.

After execution, the rename_axis() method returns a new series with renamed index 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,index=numbers)
series=series.rename_axis("Numbers")
print("The series is:")
print(series)

Output:

The series is:
Numbers
3        a
23       b
11       c
14      ab
16     abc
2     abcd
45      bc
65       d
dtype: object

In the above example, we first created a series. Then, we used the rename_axis() method to rename the index column of the series. Here, the rename_axis() method returns a new series instead of modifying the original 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.

Rename Index in a Series Inplace in Python

You can also modify the original series instead of creating a new series object after renaming the index. For this, you can set the inplace parameter to True in the rename_axis() method 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,index=numbers)
series.rename_axis("Numbers",inplace=True)
print("The series is:")
print(series)

Output:

The series is:
Numbers
3        a
23       b
11       c
14      ab
16     abc
2     abcd
45      bc
65       d
dtype: object

In this example, we have set the inplace parameter to True in the rename_axis() parameter. Hence, the index of the original series has been renamed instead of creating a new series.

Conclusion

In this article, we have discussed how to rename the index in a pandas series using the index attribute and the renam_axis() method. To know more about the 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.

Related

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.

Enroll Now

Filed Under: Basics Author: Aditya Raj

More Python Topics

API Argv Basics Beautiful Soup Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Pyspark Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehension in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Count Rows With Null Values in PySpark
  • PySpark OrderBy One or Multiple Columns
  • Select Rows with Null values in PySpark
  • PySpark Count Distinct Values in One or Multiple Columns
  • PySpark Filter Rows in a DataFrame by Condition

Copyright © 2012–2025 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us