1. Overview

In this quick tutorial, we're going to see how to drop / delete a column(s) in Pandas DataFrame.

We'll first look into using the drop method to drop a single column from DataFrame, then by using - del and df.pop and finally how to drop multiple columns.

2. Setup

In the post, we'll use the following DataFrame, which consists of several rows and columns:

import pandas as pd

df = pd.read_csv('https://raw.githubusercontent.com/softhints/Pandas-Tutorials/master/data/csv/extremes.csv')

DataFrame looks like:

Continent Highest point Elevation high Lowest point Elevation low
Asia Mount Everest 8848 Dead Sea −427
South America Aconcagua 6960 Laguna del Carbón −105
North America Denali 6198 Death Valley −86
Africa Mount Kilimanjaro 5895 Lake Assal −155
Europe Mount Elbrus 5642 Caspian Sea −28

3. Drop single column

Let's start by using the df.drop method from the DataFrame. Let's drop column 'Lowest point':

df = df.drop('Lowest point', axis=1)

or the equivalent

df = df.drop(columns='Lowest point')

By default method drop will return a copy. If you like to do the operation in place you can use the syntax above or parameter:

df.drop('Lowest point', axis=1, inplace=True)
Note:

Note that method works on both axes - `axis=1` - means columns.

After the operation the DataFrame will look like:

Continent Highest point Elevation high Elevation low
Asia Mount Everest 8848 −427
South America Aconcagua 6960 −105
North America Denali 6198 −86
Africa Mount Kilimanjaro 5895 −155
Europe Mount Elbrus 5642 −28

4. Drop multiple columns

Next let's drop several columns: "Elevation high" and "Elevation low". Again we are going to use method df.drop by providing list of columns:

df.drop(["Elevation high", "Elevation low"], axis=1)

result:

Continent Highest point
Asia Mount Everest
South America Aconcagua
North America Denali
Africa Mount Kilimanjaro
Europe Mount Elbrus

This is possible because parameter labels can be single or list-like.

Note:

Instead of using axis - `labels, axis=1` you can use parameter `columns`:

df.drop(columns=["Highest point"])

5. Drop with del and df.pop

An alternative solution to remove column from DataFrame is using the Python keyword - del:

del df["Lowest point"]
Note:

Note that this is going to delete the column in place.

One more way to achieve the same behavior is by using method df.pop:

df.pop('Highest point')

This method will return the column as series:

0 Mount Everest
1 Aconcagua
2 Denali
3 Mount Kilimanjaro
4 Mount Elbrus
5 Vinson Massif
6 Puncak Jaya
Name: Highest point, dtype: object

At the same time will remove the column from the DataFrame.

6. Resources

7. Conclusion

In this article, we looked at different solutions for dropping columns in Pandas. We saw how to drop single or multiple columns and which is the most efficient way of doing it.

The code for the examples is available over on GitHub in a Notebook.