Drop Columns from Pandas DataFrame

Here are two approaches to drop columns from Pandas DataFrame

(1) Drop a single column from the DataFrame:

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

(2) Drop multiple columns from the DataFrame:

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

The Example

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

import pandas as pd

data = {'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(data)

print(df)

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

   Color      Shape  Length  Width  Height
0   Blue     Square      15      8      30
1   Blue     Square      25      5      35
2  Green     Square      25      5      35
3  Green  Rectangle      15      4      40
4  Green  Rectangle      15      8      30
5    Red  Rectangle      15      8      35
6    Red     Square      20      5      40
7    Red  Rectangle      25      4      40

In the sections below, you’ll observe 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.drop('column name', axis=1, inplace=True)

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

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

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

import pandas as pd

data = {'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(data)

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

print(df)

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

   Color  Length  Width  Height
0   Blue      15      8      30
1   Blue      25      5      35
2  Green      25      5      35
3  Green      15      4      40
4  Green      15      8      30
5    Red      15      8      35
6    Red      20      5      40
7    Red      25      4      40

Drop Multiple Columns from Pandas DataFrame

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

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

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

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

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

import pandas as pd

data = {'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(data)

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

print(df)

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

   Color  Height
0   Blue      30
1   Blue      35
2  Green      35
3  Green      40
4  Green      30
5    Red      35
6    Red      40
7    Red      40

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

Leave a Comment