
- 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 - Insert a new index value at a specific position
To insert a new index value at a specific position, use the index.insert() method in Pandas. At first, import the required libraries -
import pandas as pd
Creating the Pandas index −
index = pd.Index(['Car','Bike','Airplane','Ship','Truck'])
Display the index −
print("Pandas Index...\n",index)
Insert a new value at a specific position using the insert() method. The first parameter in the insert() is the location where the new index value is placed. The 2 here means the new index value gets inserted at index 2 i.e. position 3. The second parameter is the new index value to be inserted.
print("\nAfter inserting a new index value...\n", index.insert(2, 'Suburban'))
Example
Following is the code −
import pandas as pd # Creating the Pandas index index = pd.Index(['Car','Bike','Airplane','Ship','Truck']) # Display the index print("Pandas Index...\n",index) # Return the dtype of the data print("\nThe dtype object...\n",index.dtype) # Insert a new value at a specific position using the insert() method # The first parameter in the insert() is the location where the new index value is placed. # The 2 here means the new index value gets inserted at index 2 i.e. position 3 # The second parameter is the new index value to be inserted. print("\nAfter inserting a new index value...\n", index.insert(2, 'Suburban'))
Output
This will produce the following output −
Pandas Index... Index(['Car', 'Bike', 'Airplane', 'Ship', 'Truck'], dtype='object') The dtype object... object After inserting a new index value... Index(['Car', 'Bike', 'Suburban', 'Airplane', 'Ship', 'Truck'], dtype='object')
- Related Articles
- Python Pandas - Insert a new index value at the first index from the last
- Python - Create a new view of the Pandas Index
- Python Pandas - Create a DataFrame from original index but enforce a new index
- How do I insert elements at a specific index in Java list?
- How to insert new row into table at a certain index using jQuery?
- Python Pandas - Return the int position of the smallest value in the Index
- Python Pandas - Return the int position of the largest value in the Index
- Python Pandas - Set only a single new specific level in a MultiIndex
- How to insert an object in an ArrayList at a specific position in java?
- Python - Make new Pandas Index with deleting multiple index elements
- Python - Search DataFrame for a specific value with pandas
- Golang program to insert a node at the ith index node, when the index is at the mid-index position in the linked list.
- Insert a Line at Specific Line Number
- Python Pandas - How to Sort MultiIndex at a specific level
- Python Pandas - Mask and replace NaNs with a specific value

Advertisements