How to drop one or multiple columns from Dataframe

The Pandas DataFrame is a versatile data structure that can handle two-dimensional labeled data with columns of different data types. The drop() method allows you to remove rows or columns by specifying the label names and the corresponding axis (0 for rows and 1 for columns) that you want to drop, or you can directly specify the index or column names to be removed.

import pandas as pd import numpy as np df = pd.DataFrame() df['Name'] = ['John', 'Doe', 'Bill'] df['Promoted'] = [True, False,True] df['Marks'] = [82, 38, 63] df
Name Promoted Marks 0 John True 82 1 Doe False 38 2 Bill True 63

Drop column "Promoted"

df = df.drop('Promoted', 1) df
Name Marks 0 John 82 1 Doe 38 2 Bill 63

where 1 is the axis number (0 for rows and 1 for columns.)

In order to delete the column without having to reassign DataFrame you can do:

df.drop('column_name', axis=1, inplace=True)

Please note that "inplace=True" was added in pandas v0.13 and it won't work on older versions.

Using Column Number

To drop columns by column number in a Pandas DataFrame, you can use the iloc method. For example, if you want to drop the columns at index 0 and 1, you can use the following syntax:

df.drop(df.columns[[0, 1]], axis=1, inplace=True)

This will remove the columns at index 0 and 1 from the DataFrame df.

df = df.drop(df.columns[[0, 2]], axis=1)
Promoted 0 True 1 False 2 True

How to drop one or multiple columns from Dataframe


The errors='ignore' parameter in the drop() method of Pandas DataFrame is available from version 0.16.1 onwards. When using errors='ignore', it allows you to drop columns only if they exist in the DataFrame. If the specified column labels are not found in the DataFrame, it will not raise an error and will simply ignore those labels while dropping columns. This can be useful to avoid errors when trying to drop columns that may or may not be present in the DataFrame.

df = df.drop('NotExist', axis=1)

When you run this code, you will get the message KeyError: "['NotExist'] not found in axis" . If you use the argument errors='ignore' then drop() method will only drop the existing columns from the labels passed to it.

df = df.drop('NotExist', axis=1,errors='ignore')


To drop one or multiple columns from a Pandas DataFrame, you can use the drop() method with the columns parameter. You can specify the column labels or column numbers that you want to drop, and use the inplace=True argument to modify the original DataFrame. Additionally, you can use errors='ignore' to drop only the existing columns and avoid errors for non-existing columns.