Built with Tutify.io

    AIStudyLabs

    Pandas Drop Duplicate Rows in DataFrame

    Pandas Drop Duplicate Rows in DataFrame

    To drop duplicate rows in a pandas DataFrame, you can use the drop_duplicates() method. Using this method you can drop duplicate rows on selected multiple columns or all columns.

    In this article, we’ll explain several ways of dropping duplicate rows from Pandas DataFrame with examples by using functions like DataFrame.drop_duplicates(), DataFrame.apply() and lambda function.

    Key Points –

    <a id="h-quick-examples-of-dropping-duplicate-rows"></a>

    Quick Examples of Dropping Duplicate Rows

    Below are quick examples of dropping duplicate rows in Pandas DataFrame.

    
    # Quick examples of drop duplicate rows
    
    # Example 1: Keep first duplicate row
    df2 = df.drop_duplicates()
    
    # Example 2: Using DataFrame.drop_duplicates() 
    # To keep first duplicate row
    df2 = df.drop_duplicates(keep='first')
    
    # Example 3: Keep last duplicate row
    df2 = df.drop_duplicates(keep='last')
    
    # Example 4: Remove all duplicate rows 
    df2 = df.drop_duplicates(keep=False)
    
    # Example 5: Delete duplicate rows based on specific columns 
    df2 = df.drop_duplicates(subset=["Courses", "Fee"], keep=False)
    
    # Example 6: Drop duplicate rows in place
    df.drop_duplicates(inplace=True)
    
    # Example 7: Using DataFrame.apply() and lambda function 
    df2 = df.apply(lambda x: x.astype(str).str.lower()).drop_duplicates(subset=['Courses', 'Fee'], keep='first')
    

    <a id="h-drop-duplicates-syntax-amp-examples"></a>

    drop_duplicates() Syntax & Examples

    Following is the syntax of the DataFrame.drop_duplicates() function that removes duplicate rows from the pandas DataFrame.

    
    # Syntax of drop_duplicates
    DataFrame.drop_duplicates(subset=None, keep='first', inplace=False, ignore_index=False)
    

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

    Parameters of the drop_duplicates()

    Following are the parameters of drop_duplicates() method.

    To run some examples of dropping duplicate rows in Pandas DataFrame, let’s create Pandas DataFrame using data from a dictionary.

    
    # drop_duplicates() Syntax & Examples
    import pandas as pd
    import numpy as np
    technologies = {
        'Courses':["Spark","PySpark","Python","pandas","Python","Spark","pandas"],
        'Fee' :[20000,25000,22000,30000,22000,20000,30000],
        'Duration':['30days','40days','35days','50days','35days','30days','50days'],
        'Discount':[1000,2300,1200,2000,1200,1000,2000]
                  }
    df = pd.DataFrame(technologies)
    print("DataFrame:\n", df)
    

    Yields below output.

    <a id="h-pandas-drop-duplicate-rows"></a>

    Pandas Drop Duplicate Rows

    To drop duplicate rows in a Pandas DataFrame, you can use the drop_duplicates() method. By default, this method removes rows with identical values across all columns while keeping the first occurrence.

    
    # keep first duplicate row
    df2 = df.drop_duplicates()
    print(df2)
    
    # Using DataFrame.drop_duplicates() to keep first duplicate row
    df2 = df.drop_duplicates(keep='first')
    print("After dropping duplicate rows:\n", df2)
    

    In the above example, drop_duplicates() is used without any arguments, which implies subset=None and keep='first'. Therefore, it removes duplicate rows based on all columns while keeping the first occurrence, resulting in a DataFrame with only unique rows. This example yields the below output.

    <a id="h-drop-duplicate-rows-and-keep-the-last-row"></a>

    Drop Duplicate Rows and Keep the Last Row

    If you want to select all the duplicate rows and their last occurrence, you must pass a keep argument as "last". For instance, df.drop_duplicates(keep='last').

    
    # Keep last duplicate row
    df2 = df.drop_duplicates( keep='last')
    print(df2)
    
    # Output:
    #    Courses    Fee Duration  Discount
    # 1  PySpark  25000   40days      2300
    # 4   Python  22000   35days      1200
    # 5    Spark  20000   30days      1000
    # 6   pandas  30000   50days      2000
    

    <a id="h-remove-all-duplicate-rows-from-pandas-dataframe"></a>

    Remove All Duplicate Rows from Pandas DataFrame

    You can set 'keep=False' in the drop_duplicates() function to remove all the duplicate rows. For E.x, df.drop_duplicates(keep=False).

    
    # Remove all duplicate rows 
    df2 = df.drop_duplicates(keep=False)
    print(df2)
    
    # Output:
    #    Courses    Fee Duration  Discount
    # 1  PySpark  25000   40days      2300
    

    <a id="h-delete-duplicate-rows-based-on-specific-columns"></a>

    Delete Duplicate Rows based on Specific Columns

    To delete duplicate rows on the basis of multiple columns, specify all column names as a list. You can set 'keep=False' in the drop_duplicates() function to remove all the duplicate rows.

    
    # Delete duplicate rows based on specific columns 
    df2 = df.drop_duplicates(subset=["Courses", "Fee"], keep=False)
    print(df2)
    
    # Output:
    #    Courses    Fee Duration  Discount
    # 1  PySpark  25000   40days      2300
    

    <a id="h-drop-duplicate-rows-in-place"></a>

    Drop Duplicate Rows In Place

    To drop duplicate rows in place in a Pandas DataFrame, you can use the drop_duplicates() method with the inplace=True parameter. This will modify the original DataFrame instead of returning a new DataFrame.

    
    # Drop duplicate rows in place
    df.drop_duplicates(inplace=True)
    print(df)
    
    # Output:
    #    Courses    Fee Duration  Discount
    # 0    Spark  20000   30days      1000
    # 1  PySpark  25000   40days      2300
    # 2   Python  22000   35days      1200
    # 3   pandas  30000   50days      2000
    

    Keep in mind that using inplace=True directly modifies the original DataFrame and does not return a new DataFrame. Therefore, there's no need to assign the result to a new variable. If you want to keep the original DataFrame unchanged and create a new one with duplicate rows dropped, you can omit inplace=True.

    <a id="h-remove-duplicate-rows-using-apply-and-lambda-function"></a>

    Remove Duplicate Rows Using apply() and Lambda Function

    You can remove duplicate rows using DataFrame.apply() and lambda function to convert the DataFrame to lower case and then apply a lower string.

    
    # Using DataFrame.apply() and lambda function 
    df2 = df.apply(lambda x: x.astype(str).str.lower()).drop_duplicates(subset=['Courses', 'Fee'], keep='first')
    print(df2)
    
    # Output:
    #    Courses    Fee Duration  Discount
    # 0    Spark  20000   30days      1000
    # 1  PySpark  25000   40days      2300
    # 2   Python  22000   35days      1200
    # 3   pandas  30000   50days      2000
    

    <a id="h-complete-example-for-drop-duplicate-rows-in-dataframe"></a>

    Complete Example For Drop Duplicate Rows in DataFrame

    
    import pandas as pd
    import numpy as np
    technologies = {
        'Courses':["Spark","PySpark","Python","pandas","Python","Spark","pandas"],
        'Fee' :[20000,25000,22000,30000,22000,20000,30000],
        'Duration':['30days','40days','35days','50days','35days','30days','50days'],
        'Discount':[1000,2300,1200,2000,1200,1000,2000]
                  }
    df = pd.DataFrame(technologies)
    print(df)
    
    # keep first duplicate row
    df2 = df.drop_duplicates()
    print(df2)
    
    # Using DataFrame.drop_duplicates() to keep first duplicate row
    df2 = df.drop_duplicates(keep='first')
    print(df2)
    
    # keep last duplicate row
    df2 = df.drop_duplicates( keep='last')
    print(df2)
    
    # Remove all duplicate rows 
    df2 = df.drop_duplicates(keep=False)
    print(df2)
    
    # Delete duplicate rows based on specific columns 
    df2 = df.drop_duplicates(subset=["Courses", "Fee"], keep=False)
    print(df2)
    
    # Drop duplicate rows in place
    df.drop_duplicates(inplace=True)
    print(df)
    
    # Using DataFrame.apply() and lambda function 
    df2 = df.apply(lambda x: x.astype(str).str.lower()).drop_duplicates(subset=['Courses', 'Fee'], keep='first')
    print(df2)
    

    <a id="h-faq-on-drop-duplicate-rows-in-dataframe"></a>

    FAQ on Drop Duplicate Rows in DataFrame

    You can use the drop_duplicates() method. By default, it removes duplicate rows based on all columns, keeping the first occurrence.

    Dropping duplicate rows means removing rows from a DataFrame that have the same values across all columns or a specified subset of columns.

    You can drop duplicates based on specific columns by specifying those columns in the subset parameter of drop_duplicates().

    You can reset the index after dropping duplicates by using the ignore_index=True parameter within the drop_duplicates() method. This will reindex the resulting DataFrame, providing a new sequential index starting from 0.

    You can easily check which rows are duplicates without dropping them by using the duplicated() method.

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

    Conclusion

    In this article, you learned various methods to drop or remove duplicate rows from a Pandas DataFrame. We explored using pandas.DataFrame.drop_duplicates() with different parameters to customize the behavior, as well as leveraging DataFrame.apply() and lambda functions for more advanced scenarios.

    Happy Learning !!

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

    Related Articles

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

    References