How to remove a specified row From the Pandas Series Using Drop() method?


The pandas series.drop() method is used to remove a specific row from the pandas series object. And It will return a series object with the removed row.

The drop() method can be applied to both labeled-based and position index abased series objects. The parameters of this drop() method are labels, axis, level, inplace, and raise.

It will raise a Key error if the specified row label is not found in the index of the series object. We can suppress the errors by setting the errors parameter from raise to ignore.

Example 1

# import pandas package
import pandas as pd

# Creating Series objects
s = pd.Series([56, 82, 43, 23, 14], index=['A', 'B', 'C', 'D', 'e'])
print('series object:',s)

result = s.drop('C')

# display the output
print(result)

Explanation

In the following example, we have created a pandas Series with a labeled index. And we removed a row named “C” from the series object by sending the label name to the drop() method.

Output

series object:
A 56
B 82
C 43
D 23
e 14
dtype: int64

A 56
B 82
D 23
e 14
dtype: int64

We have successfully removed the row “C” from the pandas series object “s”. We can see the initial series object and resultant series object in the above output block.

Example 2

# import pandas package
import pandas as pd

# Creating Series objects
s = pd.Series([38, 94, 19, 81, 74, 19, 93, 47, 31, 37])
print('series object:',s)

result = s.drop(5)

# display the output
print(result)

Explanation

Here we will how we gonna delete a row from a series object with a position-based index value. initially, we have created a pandas Series with a python list of integer values and the index label are auto-created range index values.

Output

series object:
0 38
1 94
2 19
3 81
4 74
5 19
6 93
7 47
8 31
9 37
dtype: int64

0 38
1 94
2 19
3 81
4 74
6 93
7 47
8 31
9 37
dtype: int64

We removed a row named 5 (location-based index) in the result object of the drop() method.

Updated on: 09-Mar-2022

15K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements