- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
Python Pandas - Set index name for an already created Index object
To set index name for an already created Index object, use the index.set_names() method in Pandas. At first, import the required libraries −
import pandas as pd
Creating Pandas index −
index = pd.Index(["Electronics", "Mobile Phones", "Accessories", "Home Decor", "Books"])
Display the Pandas index −
print("Pandas Index...\n",index)
Set the name of index −
print("\nSet the index name...\n",index.set_names('Products'))
Example
Following is the code −
import pandas as pd # Creating Pandas index index = pd.Index(["Electronics", "Mobile Phones", "Accessories", "Home Decor", "Books"]) # 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) # set the name of index print("\nSet the index name...\n",index.set_names('Products'))
Output
This will produce the following output −
Pandas Index... Index(['Electronics', 'Mobile Phones', 'Accessories', 'Home Decor', 'Books'], dtype='object') Number of elements in the index... 5 The dtype object... object Set the index name... Index(['Electronics', 'Mobile Phones', 'Accessories', 'Home Decor', 'Books'], dtype='object', name='Products')
- Related Articles
- Python Pandas - Set the name of the index
- Python Pandas - Alter index name
- Python Pandas - Repeat elements of an Index
- Python Pandas - Return the relative frequency from Index object
- Python Pandas - Compute indexer and mask for new index given the current index
- Python Pandas - Fill NaN values with the specified value in an Index object
- Create a Pipeline and remove a row from an already created DataFrame - Python Pandas
- Python Pandas - Create an Index based on an underlying Categorical
- Python - Make new Pandas Index with deleting multiple index elements
- Python - Check if the Pandas Index is of the object dtype
- Python Pandas - Return number of unique elements in the Index object
- How to get column index from column name in Python Pandas?
- Python Pandas - Indicate duplicate index values
- Python Pandas - Create an Index with values cast to dtypes
- Python Pandas - Create a Series with both the original index and name

Advertisements