Home

 › 

Articles

 › 

How to Drop Columns in Pandas

How to Drop Columns in Pandas

How to Drop Columns in Pandas

When working with a Pandas DataFrame, sometimes you don’t need all of your data to be present. Data analysis is usually more straightforward if we can focus our view and remove unimportant data from the equation.

There are many ways to do this, but the most common is using the drop function. Discover the simple methods for how to drop columns in Pandas with this article.

How to Drop a Column Using Drop()

First, let’s go through how to remove a single column using the drop method. This can be used to drop whatever column you wish. Follow the steps below.

Step 1: Import Pandas

how to drop columns on pandas
Import pandas as pd.

©History-Computer.com

To illustrate this, we’re going to be using Python in the Spyder environment. First, we’re going to import Pandas, so we can use it to manipulate data. This is done with this code:

 import pandas as pd

Step 2: Create a Dictionary

how to drop columns on pandas
Creating an associative array (dictionary).

©History-Computer.com

Next, we need to create a dictionary so we can illustrate the process. A dictionary is also known as an associative array, and is basically a type of data structure with key and value pairs, where every key only has one value.

data = {
'A': ['A1', 'A2', 'A3', 'A4', 'A5'],
'B': ['B1', 'B2', 'B3', 'B4', 'B5'],
'C': ['C1', 'C2', 'C3', "C4', 'C5'],
'D': ['D1', 'D2', 'D3', 'D4', 'D5'],
'E': ['E1', 'E2', 'E3', 'E4', "E5']}

This can be named whatever you like, but in this case, “data” has been chosen.

Step 3: Create the DataFrame

how to drop columns on pandas
Converting the dictionary.

©History-Computer.com

The next step is to convert the dictionary into a DataFrame. To do this, use the code:

df = pd.DataFrame(data)

Step 4: Drop the Column

How to drop columns on Pandas
Dropping the first column.

©History-Computer.com

Now, we can finally drop the column we desire. In this case, we’re going to drop the first column, labeled “A’. The code

df.drop(['A'}, axis=1)

will accomplish this. The axis is defined as 1, as this refers to columns. The output will show the DataFrame with the column removed.

Step 5: Drop Multiple Columns Using Drop()

How to drop columns on Pandas
Dropping several columns.

©History-Computer.com

This process can also be used to drop several columns with a slight modification. By using

df.drop(['A', 'B'], axis=1)

we drop columns “A” and “B” easily.

How to Drop Columns Using the Column Index

Another way to drop columns is by referring to the column index. The first column has an index of 0, the second an index of 1, and so on. This is illustrated with the steps below.

Step 1: Create DataFrame

How to drop columns on Pandas
Establishing a DataFrame.

©History-Computer.com

As before, create a DataFrame.

Step 2: Drop a Column by Column Index

How to drop columns on Pandas
Removing the first column.

©History-Computer.com

Now, we can use the code

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

to remove the first column.

Step 3: Drop Multiple Columns by Column Index

How to drop columns on Pandas
Removing several columns.

©History-Computer.com

As with the first method, we can also use this process to drop multiple columns by their index. This is done with the code

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

specifying whichever columns you wish to drop.

How to Drop Columns Using iloc[] and Drop()

An alternative way to drop columns is by using the iloc[] and drop() methods. Using the iloc method removes the first column in the specified range up to the last column specified, but excludes the last column. The next steps show how to do this.

Step 1: Create a DataFrame

How to drop columns on Pandas
Creating a DataFrame.

©History-Computer.com

We start with creating a DataFrame, like with the other methods.

Step 2: Drop Columns Using iloc[] and Drop()

How to drop columns on Pandas
Entering the code to drop columns.

©History-Computer.com

Simply input the code

df.drop(df.iloc[:, 1:3], axis=1)

specifying the range by column index. In this case, columns “B” and “C” have been removed. “D” has been retained since it’s the second column specified.

How to Drop Columns using loc[] and Drop()

With this method, we’re still dropping several columns, but using loc[] doesn’t exclude the second specified column from being dropped. Check out how to do this next.

Step 1: Create a DataFrame

How to drop columns on Pandas
Constructing a DataFrame.

©History-Computer.com

To get started, we need a DataFrame as usual.

Step 2: Drop Multiple Columns using loc[] and Drop()

How to drop columns on Pandas
Getting rid of several columns.

©History-Computer.com

With the code

df.drop(df.loc[:, 'A':'B'].columns,axis = 1)

we remove the “A” and “B” columns. As well as dropping the “B” column, also note that column labels are used with loc[] rather than the column index.

How to Delete a Column

This is a slightly different method since it uses del and not drop. The end result is similar, but the differences are that del only works for columns, and can only remove one column at a time. See how this is done below.

Step 1: Create a DataFrame

How to drop columns on Pandas
Starting out with our DataFrame.

©History-Computer.com

We’re using the same DataFrame as we’ve done previously.

Step 2: Delete a Column

How to drop columns on Pandas
Dropping the third column.

©History-Computer.com

In this case, we’re deleting the third column, which is column “C”. This is achieved with the following code:

for col in df.columns:
if 'C' in col:
del df[col]

This will delete the column from the DataFrame.

How to Delete a Column Using the Pop Function

This process works similarly to del, but differs in that it will return the deleted column back to you, whereas del doesn’t. The simple steps below this method.

Step 1: Create a DataFrame

Just as with the other methods, we need a DataFrame to begin.

Step 2: Delete a Column Using Pop

How to drop columns on Pandas
Using df.pop.

©History-Computer.com

Simply use the code

df.pop('D')

to delete the desired column, replacing “D” with your chosen column label.

How to Drop a Column from an Existing Data Table

Another way to use Pandas to drop columns is when you have a pre-existing data table. To do this, you’ll need to have your spreadsheet or table in CSV format, and know the file path for it. Follow these easy steps to accomplish this.

Step 1: Import your Data

How to drop columns on Pandas
Importing data.

©History-Computer.com

Firstly, import Pandas as normal, and then use

df = pd.read_csv(r'Filepath/Filename.csv')

to import the data. This will usually be Folder/Filename on Mac, in this case, the Documents folder. On Windows, this is normally preceded by C:/Users/Username.

Step 2: Drop Columns Using Drop()

How to drop columns on Pandas
Dropping columns using df.drop.

©History-Computer.com

As before, we use the code

df.drop([“Weight”, “Height”, axis=1)

to drop the “Weight” and “Height” columns from this table. The table is returned to you afterward, with these columns removed.

How to drop columns on Pandas
Receiving NaN values.

©History-Computer.com

It’s important to note that the column labels are case-sensitive and must be input correctly, or you’ll receive NaN values instead. This is shown in the second screenshot.

Step 3: Select Columns from the Imported Table

How to drop columns on Pandas
Selecting columns.

©History-Computer.com

This technically isn’t a way to drop columns, but a way to select them for viewing. This can be handy if you don’t want to remove the columns. Import your data as before to begin.

Step 4: Select Desired Columns

How to drop columns on Pandas
Choosing the desired columns.

©History-Computer.com

Finally, use

df = pd.DataFrame(df, columns=['Weight', 'Height'])

replacing “Weight” and “Height” with your desired column labels, to select these columns to display.

Wrapping Up

There are several methods you can use to drop one or more columns from a Pandas DataFrame. The easiest way is probably to drop() and the column labels, as this can be done to drop one or several columns in a new DataFrame or imported data.

For large volumes of data, you might want to use the iloc or loc methods instead, as these can remove many columns within a specified range. Lastly, the delete method can be used, but note that this can only remove one column at a time. All in all, once you know how to drop columns in Pandas, you can analyze your data much more efficiently.

For a helpful video on the topic, be sure to check out the video below:

Up Next

Frequently Asked Questions

How do I drop a column in Pandas?

The easiest way to drop a column is with the drop() function, and by specifying either the column label or axis. You can also use the del function.

Why would you want to drop a column?

Usually, columns are dropped because they’re not going to be useful for the analysis you plan to carry out. Removing them makes it simpler to focus on the values that are important for your analysis.

How do I remove multiple columns in Pandas?

To remove multiple columns, you need to use the drop() function but specify a range, either using column labels or column indexes.

What's the difference between drop and pop?

Using pop returns the specified column to you, whereas drop doesn’t.

What's the difference between drop and del?

Del can only be used to remove one column, and can’t be used for rows.

To top