Python | Pandas Series.drop ()

Python Methods and Functions

Series.drop() Pandas Series.drop() returns a Series with the specified index marks removed. It removes series elements based on the specified index labels.

Syntax: Series.drop (labels = None, axis = 0, index = None, columns = None, level = None, inplace = False, errors = 'raise')

Parameter:
labels: Index labels to drop.
axis: Redundant for application on Series.
index, columns: Redundant for application on Series, but index can be used instead of labels.
level: For MultiIndex, level for which the labels will be removed.
inplace: If True, do operation inplace and return None.
errors: If 'ignore', suppress error and only existing labels are dropped.

Returns: dropped: pandas.Series

Example # 1: Use Series.drop () to remove values ​​that match the passed index marks in this series object.

< p>

# import pandas as pd

import pandas as pd

 
# Create series

sr = pd.Series ([ 80 , 25 , 3 , 25 , 24 , 6 ])

 
# Create index

index_ = [  'Coca Cola' , ' Sprite' , 'Coke' , ' Fanta' , 'Dew' , ' ThumbsUp' ]

 
# set index

sr.index = index_

 
# Print series

print (sr)

Output:

Now we will use Series.drop () to remove values ​​corresponding to matching the passed index marks in this series object.

# reset passed shortcuts

result = sr.drop (labels = [ 'Sprite' , ' Dew' ]) 

 
# Print the result

print (result)

Output:


As we can see from the output, Series.drop () has successfully discarded records matching the passed index marks.

Example # 2: Use Series.drop () to remove values ​​that match the passed index marks in this series object.

# import pandas as pd

import pandas as pd

 
# Create a series

sr = pd.Series ([ 11 , 11 , 8 , 18 , 65 , 18 , 32 , 10 , 5 , 32 , 32 ])

 
# Create index

index_ = pd.date_range ( '2010-10-09' , periods = 11 , freq = 'M' )

 
# set index

sr.index = index_

 
# Print series

print (sr)

Output:

We will now use Series.drop () to remove the values ​​that match the passed index marks in this series object.

# reset passed shortcuts

result = sr.drop (labels = [pd.Timestamp ( '2010-12-31' ),

  pd.Timestamp ( ' 2011-04-30' ), pd.Timestam p ( '2011-08-31' )])

 
# Print result

print (result)

Output:


As we can see from the output, Series. drop () successfully dropped records matching passed index marks.





Tutorials