• Post author:
  • Post category:Pandas
  • Post last modified:March 27, 2024
  • Reading time:16 mins read
You are currently viewing Pandas Remove Elements From Series

Pandas Series.drop() function is used to drop/remove single/multiple elements from the Series by using label/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.

1. Quick Examples of Remove Elements From Series

If you are in a hurry below are some 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])

2. Syntax of Pandas Series.drop()

Following is the syntax of the Series.drop() function.


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

2.1 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.

2.2 Return value of drop()

The drop() returns an updated Series.

3. Initialize Pandas Series

Pandas Series is a one-dimensional, Index-labeled data structure available only in the Pandas library. It can store all the datatypes such as strings, integers, float, and other Python objects. We can access each element in the Series with the help of corresponding default indices.

Now, let’s create a Pandas series using a list of values.


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.

pandas series remove

4. Use drop() to Remove Values from Pandas 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.

pandas series remove

5. 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

6. 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

7. 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

8. Complete Examples of Remove Element From Pandas 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)

Frequently Asked Questions on Pandas Remove Elements From Series

How can I remove specific elements from a Pandas Series?

To remove specific elements from a Pandas Series, you can use boolean indexing or the drop() method.

How can I remove duplicate values from a Series?

To remove duplicate values from a Pandas Series, you can use the drop_duplicates() method. For example, the drop_duplicates() method is called on the Series series_data, and it returns a new Series with duplicate values removed. The original Series remains unchanged.

What if I want to remove elements by index label?

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.

How can I remove elements based on conditions?

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.

Can I remove elements in-place without creating a new Series?

You can remove elements from a Pandas Series in-place without creating a new Series by using the inplace=True parameter with the drop() method or by directly modifying the Series using boolean indexing.

Conclusion

In this article, I have explained how to remove single/multiple elements from a series by using Series.drop(), series.iloc[], and Series.index() functions with examples.

Happy Learning !!

Naveen Nelamali

Naveen Nelamali (NNK) is a Data Engineer with 20+ years of experience in transforming data into actionable insights. Over the years, He has honed his expertise in designing, implementing, and maintaining data pipelines with frameworks like Apache Spark, PySpark, Pandas, R, Hive and Machine Learning. Naveen journey in the field of data engineering has been a continuous learning, innovation, and a strong commitment to data integrity. In this blog, he shares his experiences with the data as he come across. Follow Naveen @ LinkedIn and Medium