In Pandas, the Series.drop()
function is used to remove one or more elements from a Series by their label or index. It returns the Series that excludes the specified index labels. In this article, I will explain how to remove single/multiple elements from the Series in Pandas by using drop()
and other methods with examples.
Key Points –
- The
drop()
method allows you to remove elements from a Series based on their labels (index values). - You can pass a list of index labels to the
drop()
function to remove multiple elements at once. - By applying a condition to the Series, you can filter out elements that do not meet the condition.
- Use
iloc[]
to remove elements based on their integer positions. - Slicing with
iloc[]
can remove rows by selecting a subset of positions.
Quick Examples of Remove Elements From Series
Following are quick examples of removing elements from a series in Python.
# Quick examples of remove elements from Series
# Example 1: Use Series.drop() function
# to remove values from series
ser2 = ser.drop(labels = ['Java','Spark','NumPy'])
# Example 2: Drop rows by index
# Use series.iloc[]
ser2 = ser.iloc[2:]
# Example 3: Use drop() function
# To remove rows
ser2 = ser.drop(ser.index[3])
# Example 4: Remove list of rows from pandas series
ser2 = ser.drop(ser.index[[1, 3, 5]])
# Example 5: Using slice interval
ser2 = ser.drop(ser.index[2:5])
Pandas Series.drop() Introduction
Following is the syntax of the Series.drop()
method.
# Syntax of Series.drop()
Series.drop(labels=None, axis=0, index=None, columns=None, level=None, inplace=False, errors='raise')
Parameters of drop()
Following are the parameters of drop() to remove the elements from the pandas Series.
lables
– single label or list-like, Index labels to drop.axis
– {0 or ‘index’} : Redundant for application on Series.index
– Redundant for application on Series, but the index can be used instead of labels.columns
– Use ‘index’ or ‘labels’ instead.level
– int or level name, optional, For MultiIndex, the level for which the labels will be dropped.inplace
– bool, default False: If True, do operation inplace and return None.errors
– {‘ignore’, ‘raise’} Default Value: ‘raise’, If ‘ignore’, suppress the error and only existing labels are removed.
Return value of drop()
The drop() returns an updated Series.
Create Pandas Series
A Pandas Series represents one-dimensional data with labeled indices, exclusive to the Pandas library. It accommodates diverse data types like strings, integers, floats, and various Python objects. Accessing individual elements within the Series relies on their associated default indices.
To run some examples of removing elements from the pandas series, let’s create a Pandas DataFrame.
import pandas as pd
# Create the Series
ser = pd.Series([20000,25000,23000,28000,55000,23000,28000])
# Create the index
index = ['Java','Spark','PySpark','Pandas','NumPy','Python',"Oracle"]
# Set the index
ser.index = index
print("Create a Pandas Series:\n", ser)
Yields below output.
Use drop() to Remove Values from Series
You can use the Pandas Series.drop()
function to drop the single/multiple elements by index from the list.
# Use Series.drop() function to remove values from series
ser2 = ser.drop(labels = ['Java','Spark','NumPy'])
print("Removing a specified elements from Series:\n", ser2)
Yields below output.
Drop Rows by Index using Series.iloc[]
By using Series.iloc[]
property we can drop the particular portion of the Series. The below example drops the 3rd value. For example.
# Drop rows by index use series.iloc[]
ser2 = ser.iloc[2:]
print("After removing a specified element from Series:\n", ser2)
Yields below output.
# Output:
After removing a specified element from Series:
PySpark 23000
Pandas 28000
NumPy 55000
Python 23000
Oracle 28000
dtype: int64
Remove Element at Specific Index
By combining drop() function and index, we can also remove rows from the pandas series at the specified index. Here I am removing the fourth element from the series.
# Use drop() function to remove rows
ser2 = ser.drop(ser.index[3])
print("After removing a specified element from Series:\n", ser2)
Yields below output.
# Output:
# After removing a specified element from Series:
Java 20000
Spark 25000
PySpark 23000
NumPy 55000
Python 23000
Oracle 28000
dtype: int64
Remove List of Rows From Pandas Series
Let’s also see how to remove multiple elements from the Pandas Series by Index number. For instance, the list [1,3,5] is passed into the drop()
function which will remove the specified list of rows from the Series.
# Remove list of rows from pandas series
ser2 = ser.drop(ser.index[[1, 3, 5]])
print("After removing a specified elements from Series:\n", ser2)
Yields below output.
# Output:
# After removing a specified elements from Series:
Java 20000
PySpark 23000
NumPy 55000
Oracle 28000
dtype: int64
Alternatively, using the drop()
function with the help of the slicing technique we can remove the rows along with the specified intervals. For examples.
# Using slice interval
ser2 = ser.drop(ser.index[2:5])
print("After removing a specified elements from Series:\n", ser2)
Yields below output.
# Output:
After removing a specified elements from Series:
Java 20000
Spark 25000
Python 23000
Oracle 28000
dtype: int64
Complete Examples of Remove Element From Series
import pandas as pd
# Create the Series
ser = pd.Series([20000,25000,23000,28000,55000,23000,28000])
# Create the Index
index = ['Java','Spark','PySpark','Pandas','NumPy','Python',"Oracle"]
# Set the index
ser.index = index
print(ser)
# Use Series.drop() function
# To remove values from series
ser2 = ser.drop(labels = ['Java','Spark','NumPy'])
print(ser2)
# Drop rows by index
# Use series.iloc[]
ser2 = ser.iloc[2:]
print(ser2)
# Use drop() function to remove rows
ser2 = ser.drop(ser.index[3])
print(ser2)
# Remove list of rows from pandas series
ser2 = ser.drop(ser.index[[1, 3, 5]])
print(ser2)
# Using slice interval
ser2 = ser.drop(ser.index[2:5])
print(ser2)
FAQ on Remove Elements From Series
To remove specific elements from a Pandas Series, you can use boolean indexing or the drop()
method.
If you want to remove elements from a Pandas Series by their index labels, you can use the drop()
method. For example, the drop()
method is used with a list of index labels ['b', 'd']
to remove the corresponding elements. The resulting Series will not contain the elements with the specified index labels.
To remove elements from a Pandas Series based on conditions, you can use boolean indexing. For example, the boolean condition series_data <= 3
is used for indexing. It selects only the elements that satisfy the condition (i.e., values less than or equal to 3). The resulting Series will not include elements that do not meet the specified condition.
Conclusion
In this article, you have learned to remove single/multiple elements from a series by using Series.drop()
, series.iloc[]
, and Series.index()
functions with some examples.
Happy Learning !!
Related Articles
- Convert Pandas DataFrame to Series
- How to Plot the Pandas Series
- Pandas Iterate Over Serie
- How to replace the Pandas Series?
- How to append Pandas Series?
- How to reshape the Pandas Series?
- Convert Series to Dictionary(Dict) in Pandas
- Pandas Stack Two Series Vertically and Horizontally
- Find Intersection Between Two Series in Pandas
- How to Plot Columns of Pandas DataFrame