Skip to content
Home » How to: pandas – drop column

How to: pandas – drop column

There are many ways to remove a column in a pandas DataFrame. However, some ways are better than others. In this blog post, I elaborate on multiple solutions and what the pros and cons are.

First, let’s load the iris dataset from the Seaborn package on GitHub.

import pandas as pd
df = pd.read_csv('https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv')

Drop a pandas DataFrame column using drop()

The drop method is probably the most convenient and straightforward way to get rid of a column in a pandas DataFrame. It has been through some changes in the beta versions of pandas and now provides two ways to specify a column.

The traditional way is by specifying the column name(s) and setting the axis parameter to 1. By setting the inplace parameter to True, we remove the column without having to reassign the DataFrame. If you don’t set the axis parameter, you’ll more than likely run into a KeyError.

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

Recent versions of Pandas now support specifying the columns through the columns parameter.

df.drop(columns = 'sepal_length', inplace = True)

To remove a pandas DataFrame column by its index number, you can do it by referring to the DataFrame property columns, which will be returned as a list. By subsetting that list through your desired index numbers, you can pass them to the drop method. The following example removes the first two columns.

df.drop(columns = df.columns[0:2], inplace = True)

Drop a pandas DataFrame column using pop()

I’m not sure how many times I have used the pop method, but I can definitely count it on one hand. It only works with one column. There’s no way to specify a list of column names. It always returns the column that you remove and will store the DataFrame in place, without the removed column.

df.pop('sepal_length')

Drop a pandas DataFrame column using del

An alternative to the drop and pop methods in pandasm is by using native del. It’s not best practice — not at all — but it definitely works. Make sure you remove the column by specifying it through bracket notation, and not through dot notation.

del df['sepal_length'] # this will work
del df.sepal_length # this will not work

If you use dot notation, you will get an AttributeError. The explanation is confusing — and I still don’t comprehend the full story — but by using dot notation you can refer to not only columns but also attributes. In theory, it could work, but it was designed not to. A full explanation can be found here.

Say thanks, ask questions or give feedback

Technologies get updated, syntax changes and honestly… I make mistakes too. If something is incorrect, incomplete or doesn’t work, let me know in the comments below and help thousands of visitors.

1 thought on “How to: pandas – drop column”

  1. Monitor phone from anywhere and see what’s happening on target phone. You will be able to monitor and store call logs, messages, social activities , images , videos, whatsapp and more. Real-time monitoring of phones, No technical knowledge is required, no root is required.

Leave a Reply

Your email address will not be published. Required fields are marked *