- Trending Categories
- Data Structure
- Operating System
- MS Excel
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
# 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)
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.
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.
# 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)
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.
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.
- Related Articles
- How to remove NaN from a Pandas Series?
- Python Pandas - Remove the specified categories from CategoricalIndex
- How to drop duplicate rows in pandas series?
- How to retrieve the last valid index from a series object using pandas series.last_valid_index() method?
- How to remove a group of elements from a pandas series object?
- How to create a series from a list using Pandas?
- How to unnest (explode) a row in a pandas series?
- How to combine a pandas Series with Scalar using Series.combine() method?
- How to divide a pandas series with scalar using series.div() method?
- Python - How to drop the null rows from a Pandas DataFrame
- How to remove duplicates in series from each row in an R data frame?
- How to get the final rows of a time series data using pandas series.last() method?
- How to remove rows in a Pandas series with duplicate indices?
- How to get items from a series object using the get() method?
- How to remove a row from matrix in R by using its name?