How to Drop Columns from Pandas DataFrame

Here are two approaches to drop columns from Pandas DataFrame

(1) Drop a single column from the DataFrame:

df = df.drop('column name',axis=1)

(2) Drop multiple columns from the DataFrame:

df = df.drop(['column 1','column 2','column 3',...],axis=1)

In the next section, you’ll see how to apply the above two approaches using a simple example.

The Example

To start with a simple example, let’s create a DataFrame with 5 columns about boxes:

import pandas as pd

boxes = {'Color': ['Blue','Blue','Green','Green','Green','Red','Red','Red'],
         'Shape': ['Square','Square','Square','Rectangle','Rectangle','Rectangle','Square','Rectangle'],
        'Length': [15,25,25,15,15,15,20,25],
         'Width': [8,5,5,4,8,8,5,4],
        'Height': [30,35,35,40,30,35,40,40]
        }

df = pd.DataFrame(boxes, columns = ['Color','Shape','Length','Width','Height'])

print (df)

Run the code in Python, and you’ll get the following DataFrame:

Example of a DataFrame

In the sections below, you’ll see how to drop:

  • A single column from the DataFrame
  • Multiple columns from the DataFrame

Drop a Single Column from Pandas DataFrame

Here is the approach that you can use to drop a single column from the DataFrame:

df = df.drop('column name',axis=1)

For example, let’s drop the ‘Shape‘ column. To do that, simply add the following syntax:

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

So the complete Python code to drop the ‘Shape’ column is:

import pandas as pd

boxes = {'Color': ['Blue','Blue','Green','Green','Green','Red','Red','Red'],
         'Shape': ['Square','Square','Square','Rectangle','Rectangle','Rectangle','Square','Rectangle'],
        'Length': [15,25,25,15,15,15,20,25],
         'Width': [8,5,5,4,8,8,5,4],
        'Height': [30,35,35,40,30,35,40,40]
        }

df = pd.DataFrame(boxes, columns = ['Color','Shape','Length','Width','Height'])

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

print (df)

As you can see, the ‘Shape’ column no longer exists in the DataFrame:

How to Drop Columns from Pandas DataFrame

Drop Multiple Columns from Pandas DataFrame

You can use this template in order to drop multiple columns from your DataFrame:

df = df.drop(['column 1','column 2','column 3',...],axis=1)

For instance, let’s drop the Shape, Length and Width columns by adding this syntax:

df = df.drop(['Shape','Length','Width'],axis=1)

So the full Python code to drop the 3 columns is:

import pandas as pd

boxes = {'Color': ['Blue','Blue','Green','Green','Green','Red','Red','Red'],
         'Shape': ['Square','Square','Square','Rectangle','Rectangle','Rectangle','Square','Rectangle'],
        'Length': [15,25,25,15,15,15,20,25],
         'Width': [8,5,5,4,8,8,5,4],
        'Height': [30,35,35,40,30,35,40,40]
        }

df = pd.DataFrame(boxes, columns = ['Color','Shape','Length','Width','Height'])

df = df.drop(['Shape','Length','Width'],axis=1)

print (df)

You’ll now see that the Shape, Length and Width columns are no longer present in the DataFrame:

Drop Columns from Pandas DataFrame

You can visit the Pandas Documentation to learn more about df.drop.