
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python Pandas - Alter index name
To alter index name, use the index.rename() method in Pandas. At first, import the required libraries −
import pandas as pd
Creating Pandas index −
index = pd.Index(['Car','Bike','Airplane', 'Ship','Truck','Suburban'], name ='Transport')
Display the Pandas index −
print("Pandas Index...\n",index)
Rename the index −
print("\nRename the index...\n",index.rename('Mode_of_Transport'))
Example
Following is the code −
import pandas as pd # Creating Pandas index index = pd.Index(['Car','Bike','Airplane', 'Ship','Truck','Suburban'], name ='Transport') # Display the Pandas index print("Pandas Index...\n",index) # Return the number of elements in the Index print("\nNumber of elements in the index...\n",index.size) # Return the dtype of the data print("\nThe dtype object...\n",index.dtype) # Rename the index print("\nRename the index...\n",index.rename('Mode_of_Transport'))
Output
This will produce the following output −
Pandas Index... Index(['Car', 'Bike', 'Airplane', 'Ship', 'Truck', 'Suburban'], dtype='object', name='Transport') Number of elements in the index... 6 The dtype object... object Rename the index... Index(['Car', 'Bike', 'Airplane', 'Ship', 'Truck', 'Suburban'], dtype='object', name='Mode_of_Transport')
- Related Articles
- Python Pandas - Set the name of the index
- Python Pandas - Set index name for an already created Index object
- How to get column index from column name in Python Pandas?
- Python Pandas - Create a Series with both the original index and name
- Python Pandas - Create a DataFrame with both the original index and name
- Python Pandas - Indicate duplicate index values
- Python - Make new Pandas Index with deleting multiple index elements
- Python Pandas - Check if the Pandas Index holds Interval objects
- Sort index in ascending order – Python Pandas
- Python Pandas - Repeat elements of an Index
- Python Pandas - Return Index without NaN values
- Python - How to reset index after Groupby pandas?
- Python Pandas - Return unique values in the index
- Python Pandas - Return the Transpose of the index
- Python Pandas - Check if the index has NaNs

Advertisements