Built with Tutify.io

    AIStudyLabs

    Pandas Drop Multiple Columns From DataFrame

    Pandas Drop Multiple Columns From DataFrame

    In this pandas drop multiple columns article, I will explain how to remove/delete/drop multiple columns from DataFrame with examples. drop() method is used to remove columns and rows according to the specific column(label) names and indexes corresponding axes.

    Note that the drop() method by default returns a DataFrame(copy) after dropping specified columns. In case you want to remove columns in place then you should use inplace=True.

    Key Points –

    <a id="h-quick-examples-of-pandas-drop-multiple-columns"></a>

    Quick Examples of Pandas Drop Multiple Columns

    Below are some quick examples of how to drop multiple columns from Pandas DataFrame.

    
    # Quick examples of pandas drop multiple columns
    
    # Drop multiple columns by Name
    df.drop(["Courses", "Fee"], axis = 1, inplace=True)
    
    # Drop multiple columns by Index
    df.drop(df.columns[[1,2]], axis = 1, inplace=True)
    
    # Drop multiple columns between two columns
    df.loc[:, 'Courses':'Fee'].columns, axis = 1, inplace=True)
    
    # Drop multiple columns between two indexes
    df.drop(df.iloc[:, 1:2], axis=1, inplace=True)
    

    <a id="h-pandas-drop-multiple-columns"></a>

    Pandas Drop Multiple Columns

    In Pandas Dataframe sometimes you would be required to drop multiple columns by name, index, within the range of index, and between two columns, all these could be done using the drop() method.

    drop() method removes the columns from the DataFrame, by default it doesn't remove the existing DataFrame instead it returns a new DataFrame without the columns specified with the drop method. In order to remove columns on the existing DataFrame object use inplace=True param.

    If a column you want to remove is not present on the DataFrame it returns an error message and you can handle this error using errors param.

    You can also drop the index of the DataFrame using index param.

    Now, Let's learn with examples. first, [create a DataFrame with a dictionary](http://create a pandas DataFrame with a dictionary) of lists. On our DataFrame, we have columns Courses, Fee and Duration.

    
    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']
                  })
    df = pd.DataFrame(technologies)
    print(df)
    

    Yields below output.

    <a id="h-drop-multiple-columns-by-name"></a>

    Drop Multiple Columns By Name

    When you have a list of columns to drop, create a list object with the column's name and use it with the drop() method or directly use the list. The Below examples delete columns Courses and Fee from Pandas DataFrame.

    
    # Drop Multiple Columns By Name
    df2=df.drop(["Courses", "Fee"], axis = 1)
    print("After dropping the columns of DataFrame:\n", df2)
    

    Yields below output. Use inplace=True to update the self DataFrame.

    Related: You can also use the drop() function to drop the first and the last column of the DataFrame.

    <a id="h-drop-multiple-columns-by-index"></a>

    Drop Multiple Columns by Index

    If you wanted to drop multiple columns by index, unfortunately, the drop() method doesn't take an index as a param, but we can overcome this by getting column names by index using df.columns[]. Use the below example to delete columns 0 and 1 (index starts from 0) index. for more examples, refer to remove multiple columns by index.

    
    # Drop Multiple Columns by Index
    df2=df.drop(df.columns[[0,1]], axis = 1)
    print(df2)
    

    Yields the same output as above.

    <a id="h-drop-columns-from-the-list"></a>

    Drop Columns from the List

    If you have a list of columns and you wanted to delete all columns from the list, use the below approach.

    
    # Drop Columns from List 
    lisCol = ["Courses","Fee"]
    df2=df.drop(lisCol, axis = 1)
    print(df2)
    

    <a id="h-remove-columns-between-specified-columns"></a>

    Remove Columns Between Specified Columns

    You can use drop() method with the loc[] attribute of Pandas to remove a specific portion of the columns given DataFrame. Use [ : , 'Courses':'Fee'] to drop the one and second columns. You can use inplace the param of the drop() function to implement this code on the original object.

    
    # Remove Columns Between Specified Columns
    df.drop(df.loc[:, 'Courses':'Fee'].columns, axis = 1, inplace=True)
    print(df)
    

    <a id="h-remove-columns-between-specified-indexes"></a>

    Remove Columns Between Specified Indexes

    Alternatively, you can use the drop() method with iloc[] an attribute to remove all columns between specific columns. Use [:, 1:2] for deleting the second column. For instance, df.drop(df.iloc[:, 1:2], inplace=True, axis=1), removes Fee column.

    
    # Remove Columns Between Specified Indexes. 
    df.drop(df.iloc[:, 1:2], inplace=True, axis=1)
    print(df)
    

    <a id="h-complete-example-for-reference"></a>

    Complete Example For Reference

    
    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']
                  })
    df = pd.DataFrame(technologies)
    print(df)
    
    # Drop multiple columns by Name
    df2=df.drop(["Courses", "Fee"], axis = 1)
    print(df2)
    
    # Drop multiple columns by Index
    df2=df.drop(df.columns[[0,1]], axis = 1)
    print(df2)
    
    # Drop Columns from List
    lisCol = ["Courses","Fee"]
    df2=df.drop(lisCol, axis = 1)
    print(df2)
    
    # Drop columns between two columns
    df2=df.drop(df.loc[:, 'Courses':'Fee'].columns, axis = 1)
    print(df)
    
    # Remove Columns Between Specified Indexes 
    df.drop(df.iloc[:, 1:2], inplace=True, axis=1)
    print(df)
    

    <a id="h-faq-on-pandas-drop-multiple-columns-from-dataframe"></a>

    FAQ on Pandas Drop Multiple Columns From DataFrame

    To drop multiple columns from a Pandas DataFrame, use the drop() method and specify the column names in a list. You can also set the axis=1 parameter to indicate that you're dropping columns (not rows).

    You can drop columns using their index positions by first selecting the column names via df.columns and then passing the desired index positions to drop().

    You can drop multiple columns in place without creating a new DataFrame by setting the inplace parameter to True in the drop() method. This modifies the original DataFrame directly.

    To drop columns using column index positions instead of column names, you can use the df.columns attribute to access the column names by index and then drop them.

    If you try to drop a column that doesn't exist in the DataFrame, Pandas will raise a KeyError. To avoid this, you can pass the errors='ignore' argument, which will silently ignore any missing columns.

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

    Conclusion

    In this pandas drop multiple columns article, you have learned how to remove or delete multiple columns from DataFrame by name, label, and index. Also, you have learned how to remove columns between two columns and many more examples.

    Happy Learning !!

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

    Related Articles