How do i delete multiple rows in a dataframe in python?

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Pandas provide data analysts a way to delete and filter data frame using .drop() method. Rows can be removed using index label or column name using this method.
     

    Syntax: 
    DataFrame.drop(labels=None, axis=0, index=None, columns=None, level=None, inplace=False, errors=’raise’)
    Parameters: 
    labels: String or list of strings referring row or column name. 
    axis: int or string value, 0 ‘index’ for Rows and 1 ‘columns’ for Columns. 
    index or columns: Single label or list. index or columns are an alternative to axis and cannot be used together. 
    level: Used to specify level in case data frame is having multiple level index. 
    inplace: Makes changes in original Data Frame if True. 
    errors: Ignores error if any value from the list doesn’t exists and drops rest of the values when errors = ‘ignore’
    Return type: Dataframe with dropped values 
     

    Now, Let’s create a sample dataframe 
     

    Python3

    import pandas as pd

    details = {

        'Name' : ['Ankit', 'Aishwarya', 'Shaurya','Shivangi'],

        'Age' : [23, 21, 22,21],

        'University' : ['BHU', 'JNU', 'DU', 'BHU'],

    }

    df = pd.DataFrame(details,columns = ['Name','Age','University'],

                      index = ['a', 'b', 'c', 'd'])

    df

    Output:
     

    How do i delete multiple rows in a dataframe in python?

    Example #1: Delete a single Row in DataFrame by Row Index Label 
     

    Python3

    import pandas as pd

    details = {

        'Name' : ['Ankit', 'Aishwarya', 'Shaurya', 'Shivangi'],

        'Age' : [23, 21, 22, 21],

        'University' : ['BHU', 'JNU', 'DU', 'BHU'],

    }

    df = pd.DataFrame(details, columns = ['Name', 'Age', 'University'],

                      index = ['a', 'b', 'c', 'd'])

    update_df = df.drop('c')

    update_df

    Output :
     

    How do i delete multiple rows in a dataframe in python?

    Example #2: Delete Multiple Rows in DataFrame by Index Labels 
     

    Python3

    import pandas as pd

    details = {

        'Name' : ['Ankit', 'Aishwarya', 'Shaurya', 'Shivangi'],

        'Age' : [23, 21, 22, 21],

        'University' : ['BHU', 'JNU', 'DU', 'BHU'],

    }

    df = pd.DataFrame(details, columns = ['Name', 'Age', 'University'],

                      index = ['a', 'b', 'c', 'd'])

    update_df = df.drop(['b', 'c'])

    update_df

    Output :
     

    How do i delete multiple rows in a dataframe in python?

    Example #3: Delete a Multiple Rows by Index Position in DataFrame 
     

    Python3

    import pandas as pd

    details = {

        'Name' : ['Ankit', 'Aishwarya', 'Shaurya', 'Shivangi'],

        'Age' : [23, 21, 22, 21],

        'University' : ['BHU', 'JNU', 'DU', 'BHU'],

    }

    df = pd.DataFrame(details, columns = ['Name', 'Age', 'University'],

                      index = ['a', 'b', 'c', 'd'])

    update_df = df.drop([df.index[1], df.index[2]])

    update_df

    Output :
     

    How do i delete multiple rows in a dataframe in python?

    Example #4: Delete rows from dataFrame in Place 
     

    Python3

    import pandas as pd

    details = {

        'Name' : ['Ankit', 'Aishwarya', 'Shaurya', 'Shivangi'],

        'Age' : [23, 21, 22, 21],

        'University' : ['BHU', 'JNU', 'DU', 'BHU'],

    }

    df = pd.DataFrame(details, columns = ['Name', 'Age', 'University'],

                      index = ['a', 'b', 'c', 'd'])

    df.drop(['c', 'd'], inplace = True )

    df

    Output :
     

    How do i delete multiple rows in a dataframe in python?


    How do I remove rows from a DataFrame in Python?

    To drop a row or column in a dataframe, you need to use the drop() method available in the dataframe. You can read more about the drop() method in the docs here. Rows are labelled using the index number starting with 0, by default. Columns are labelled using names.

    How do I delete first 10 rows in pandas?

    Use drop() to remove first N rows of pandas dataframe To make sure that it removes the rows only, use argument axis=0 and to make changes in place i.e. in calling dataframe object, pass argument inplace=True.

    How do you remove unwanted rows in Python?

    To delete a row from a DataFrame, use the drop() method and set the index label as the parameter.

    Which command will be used to delete 3 and 5 rows of the data frame?

    It's all about the “DataFrame drop” command. The drop function allows the removal of rows and columns from your DataFrame, and once you've used it a few times, you'll have no issues. The Pandas “drop” function is used to delete columns or rows from a Pandas DataFrame.