
- 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 - Make new Index with passed list of labels deleted
To make new Index with passed list of labels deleted, use the index.drop() method. Pass the list of labels in it.
At first, import the required libraries −
import pandas as pd
Creating the index −
index = pd.Index(['Car','Bike','Truck','Ship','Airplane'])
Display the index −
print("Pandas Index...\n",index)
A list containing labels to be dropped are passed −
print("\nUpdated index after deleting labels...\n",index.drop(['Bike', 'Ship']))
Example
Following is the code −
import pandas as pd # Creating the index index = pd.Index(['Car','Bike','Truck','Ship','Airplane']) # Display the index print("Pandas Index...\n",index) # Return the dtype of the data print("\nThe dtype object...\n",index.dtype) # Return a tuple of the shape of the underlying data print("\nA tuple of the shape of underlying data...\n",index.shape) # a list containing labels to be dropped are passed print("\nUpdated index after deleting labels...\n",index.drop(['Bike', 'Ship']))
Output
This will produce the following code −
Pandas Index... Index(['Car', 'Bike', 'Truck', 'Ship', 'Airplane'], dtype='object') The dtype object... object A tuple of the shape of underlying data... (5,) Updated index after deleting labels... Index(['Car', 'Truck', 'Airplane'], dtype='object')
- Related Articles
- Python - Make new Pandas Index with passed location deleted
- Python - Make new Pandas Index with deleting multiple index elements
- How to select subset of data with Index Labels in Python Pandas?
- Python Pandas - Return the label from the index if all of the labels in the index are later than the passed label
- Python - Create a new view of the Pandas Index
- Python Pandas - Return a new Index of the values set with the mask
- Python Pandas - Return a new Index with elements of index not in other and get the difference
- Python Pandas - Return a new Index with elements of index not in other but unsort the result
- Python Pandas - Return a list of the Index values
- Python Pandas - Create a DataFrame from original index but enforce a new index
- Python Pandas - Compute indexer and mask for new index given the current index
- Python Pandas - Insert a new index value at the first index from the last
- Python Pandas - Insert a new index value at a specific position
- Python Pandas - Return a new Index of the values selected by the indices
- How to suffix a string to pandas series index labels?

Advertisements