Built with Tutify.io

    AIStudyLabs

    Pandas - Drop List of Rows From DataFrame

    Pandas - Drop List of Rows From DataFrame

    By using pandas.DataFrame.drop() method you can remove/delete/drop the list of rows from pandas, all you need to provide is a list of rows indexes or labels as a param to this method. By default drop() method removes the rows and returns a copy of the updated DataFrame instead of replacing the existing referring DataFrame. If you want to remove from the DataFrame in place use inplace=True param. By default, the value for inplace the property is False meaning not to update the existing DataFrame.

    By using the same drop() method, you can also remove columns in pandas DataFrame by using axis=1.

    Key Points –

    <a id="h-1-create-a-sample-dataframe"></a>

    1. Create a Sample DataFrame

    Let's create a pandas DataFrame to explain how to remove the list of rows with examples, my DataFrame contains the column names Courses, Fee, Duration, and Discount.

    
    # Create a Sample DataFrame
    import pandas as pd
    technologies = {
        'Courses':["Spark","PySpark","Hadoop","Python","pandas","Oracle","Java"],
        'Fee' :[20000,25000,26000,22000,24000,21000,22000],
        'Duration':['30day','40days','35days', '40days','60days','50days','55days'],
        'Discount':[1000,2300,1500,1200,2500,2100,2000]
                   }
    df = pd.DataFrame(technologies)
    print("DataFrame:\n", df)
    

    Yields below output.

    <a id="h-2-pandas-drop-list-of-rows-by-index"></a>

    2. pandas Drop List of Rows by Index

    pandas.DataFrame.drop() function takes a list of indexes/labels that you wanted to delete, this function returns a copy of DataFrame without modifying the reference DataFrame.

    Related: In Pandas, you can drop rows from DataFrame by index position.

    
    # Drop by List of Index position
    df1 = df.drop([2,4])
    print("After drooping list of rows:\n", df1)
    

    Yields below output.

    If you want to create a new index after removing the list of rows.

    
    # Reset index
    df = pd.DataFrame(technologies)
    df1 = df.drop([2,4]).reset_index()
    print(df1)
    

    <a id="h-4-drop-a-list-of-rows-from-dataframe-inplace"></a>

    4. Drop a List of Rows From DataFrame inplace

    If you notice by default drop() method returns the copy of the DataFrame after removing rows, but if you want to update the existing DataFrame, use inplace=True the parameter. when you use inplace=True param, DataFrame returns None instead of DataFrame. For E.x df.drop([3,5], inplace=True) drops the specified list of rows from df an object.

    
    # Drop a List of Rows From DataFrame inplace
    df = pd.DataFrame(technologies)
    df.drop([2,4],inplace=True)
    print(df)
    

    Yields the same output as above.

    <a id="h-5-removing-range-of-rows-from-one-to-five"></a>

    5. Removing Range of Rows From One to Five

    You can use Python list slicing to delete a list of rows from 1 to 5 for example, df.drop(df.index[:5],inplace= True) function remove one to five rows.

    
    # Removing Range of Rows From One to Five
    df = pd.DataFrame(technologies)
    df.drop(df.index[:5],inplace=True)
    print(df)
    

    Yields below output.

    
    # Output:
    Removing Range of Rows from 0 to 5
      Courses    Fee Duration  Discount
    5  Oracle  21000   50days      2100
    6    Java  22000   55days      2000
    

    <a id="h-6-drop-list-of-rows-by-index-position-in-dataframe"></a>

    6. Drop List of Rows by Index Position in DataFrame

    In case if you have index labels on DataFrame, you can also remove the list of rows by position. For example, below removes 3rd and 5th records from DataFrame. Note index starts from zero.

    
    # Drop List of Rows by Index Position in DataFrame
    indexes=['r1','r2','r3','r4','r5','r6','r7']
    df = pd.DataFrame(technologies,index=indexes)
    df.drop(df.index[[2,4]],inplace=True)
    print(df)
    

    Yields below output.

    
    # Output:
        Courses    Fee Duration  Discount
    r1    Spark  20000    30day      1000
    r2  PySpark  25000   40days      2300
    r4   Python  22000   40days      1200
    r6   Oracle  21000   50days      2100
    r7     Java  22000   55days      2000
    

    You can also try removing the rows by applying pandas filter.

    Frequently Asked Questions on

    The drop() method in Pandas DataFrame is used to remove rows or columns from the DataFrame based on specified index labels or positions. By default, it removes rows, but you can specify the axis parameter to remove columns instead.

    You can drop multiple rows at once using the drop() method in Pandas. You can achieve this by passing a list of index labels or positions corresponding to the rows you want to drop.

    Parameters such as labels or index are crucial for specifying which rows to drop based on either index labels or positions. Additionally, the axis parameter is essential to indicate that rows (axis=0) are being targeted for removal.

    Set the inplace parameter to True when calling the drop() method. This ensures that modifications are made directly to the original DataFrame rather than creating a new one.

    You can drop rows based on conditions using methods like drop() combined with boolean indexing or the loc[] accessor to filter rows based on specific criteria before dropping them.

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

    Conclusion

    In this article, you have learned how to remove a list of DataFrame rows in pandas using the drop() function, also learned how to remove rows by a list of indexes and labels.

    Happy Learning !!

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

    Related Articles

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

    References