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

©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

©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

©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

©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()

©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

©History-Computer.com
As before, create a DataFrame.
Step 2: Drop a Column by Column Index

©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

©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

©History-Computer.com
We start with creating a DataFrame, like with the other methods.
Step 2: Drop Columns Using iloc[] and Drop()

©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

©History-Computer.com
To get started, we need a DataFrame as usual.
Step 2: Drop Multiple Columns using loc[] and Drop()

©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

©History-Computer.com
We’re using the same DataFrame as we’ve done previously.
Step 2: Delete a 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

©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

©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()

©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.

©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

©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

©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
- How to Make a Twitter Bot: A Step-By-Step Guide to Building a Twitter Bot in Python
- BASIC Programming Language: History, Origin, and More
- What’s the Difference Between a Frontend and Backend Developer?
The image featured at the top of this post is ©ConceptCafe/Shutterstock.com.