Built with Tutify.io

    AIStudyLabs

    Pandas Remove Elements From Series

    Pandas Remove Elements From Series

    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 -

    <a id="h-quick-examples-of-remove-elements-from-series"></a>

    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])
    

    <a id="h-pandas-series-drop-introduction"></a>

    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')
    

    <a id="h-parameters-of-drop"></a>

    Parameters of drop()

    Following are the parameters of drop() to remove the elements from the pandas Series.

    <a id="h-return-value-of-drop"></a>

    Return value of drop()

    The drop() returns an updated Series.

    <a id="h-create-pandas-series"></a>

    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.

    <a id="h-use-drop-to-remove-values-from-series"></a>

    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.

    <a id="h-drop-rows-by-index-using-series-iloc"></a>

    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
    

    <a id="h-remove-element-at-specific-index"></a>

    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
    

    <a id="h-remove-list-of-rows-from-pandas-series"></a>

    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
    

    <a id="h-complete-examples-of-remove-element-from-series"></a>

    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)
    

    <a id="h-faq-on-remove-elements-from-series"></a>

    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.

    <a id="h-conclusion"></a>

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

    <a id="h-related-articles"></a>

    Related Articles