Change language

Python | Pandas Index.delete ()

| |

Index.delete() Pandas Index.delete() returns a new object with passed locations removed. We can pass more than one place to delete as a list.

Syntax: Index.delete (loc)

Parameters:
loc: Scalar / List of Indices

Returns: new_index: Index

Example # 1: Use Index.delete () to delete the first value in the index.

# import pandas as pd

import pandas as pd

 
# Create index

idx = pd.Index ([ ’Jan’ , ’Feb’ , ’ Mar’ , ’Apr’ , ’May’ , ’ Jun’ ,

’Jul’ , ’Aug’ , ’ Sep’ , ’Oct’ , ’ Nov’ , ’Dec’ ])

  
# Print index
idx

Output:

Let’s delete the month" January ". It’s at index 0, so we’ll pass 0 as an argument to the function.

# remove the first tag in this index

idx.delete ( 0 )

Output:

As we can see in the output, the function returned an object with the first label removed.

Example # 2: Use Index.delete () to delete more than one shortcut in an index.

# import pandas as pd

import pandas as pd

  
# Create index

idx = pd.Index ([ ’Jan’ , ’Feb’ , ’ Mar’ , ’Apr’ , ’ May’ , ’Jun’ ,

  ’ Jul’ , ’Aug’ , ’Sep’ , ’Oct’ , ’ Nov’ , ’Dec’ ])

  
# Print index
idx

Output:

Let’s remove the second, third, fourth and fifth indexes from the index. We pass the list of values ​​to remove to the function.

# remove the values ​​present at the 2nd, 3rd, 4th and 5th places in the index.

idx.delete ([ 2 , 3 , 4 , 5 ])

Output:

How we can see that the labels corresponding to the passed values ​​in the index have been removed.

Python | Pandas Index.delete () __del__: Questions

Python | Pandas Index.delete () __delete__: Questions

Shop

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method