Python developers often use Pandas dataframe to store data in a tabular manner as columns and rows. Often, they need to delete one or more columns in Pandas dataframe. There are several ways to do this. In this article, we will learn how to delete column in Pandas dataframe.
How to Delete Column in Pandas Dataframe
There are 3 principle ways to delete one or more columns in dataframe. Let us look at them one by one.
1. Using del
Python provides del command to delete columns in Pandas dataframe. Here is its syntax.
del dataframe[column_name]
Here is an example to delete column ‘calories’ from a dataframe. Let us first create a dataframe.
import pandas as pd
data = {
"calories": [420, 380, 390],
"duration": [50, 40, 45]
}
df = pd.DataFrame(data)
print(df)
Here is the output.
calories duration
0 420 50
1 380 40
2 390 45
Here is the code to delete column ‘calories’.
del df['calories']
print(df)
Here is the output.
duration
0 50
1 40
2 45
This is an easy to way to delete a column in Pandas dataframe. But it allows you to delete only one column at a time. If you want to delete multiple columns or collection or columns then you need to use the drop command described below.
2. Using drop
Every dataframe supports drop command out of the box. You can directly call it on every dataframe. It is a very versatile command that allows you to remove one or more columns as per your requirement. Let us look at the different ways to delete one or more columns. Let us say you have the following dataframe.
import pandas as pd
data = {
"calories": [420, 380, 390],
"duration": [50, 40, 45],
"id": [1, 2, 3]
}
#load data into a DataFrame object:
df = pd.DataFrame(data)
print(df)
Here is the output.
calories duration id
0 420 50 1
1 380 40 2
2 390 45 3
Here is a simple way to delete one column by specifying its column name.
dataframe = dataframe.drop('column_name', axis=1)
Here is an example to drop ‘calories’ column.
df = df.drop('calories', axis=1)
print(df)
## output ##
duration id
0 50 1
1 40 2
2 45 3
By default, when you call df.drop() command, it will return the result dataframe, without modifying the original dataframe. So we re-assign the result back to the original dataframe, in order to modify it. If you do not want to re-assign the dataframe, then you can modify the drop command as shown below, using the ‘inplace’ keyword.
df.drop('calories', axis=1, inplace=True)
print(df)
In the above examples, we are deleting only one column. If you want to delete multiple columns, you need to use ‘columns’ argument and pass the list of names of columns that you want to delete. Here is an example to delete columns calories and id.
df = df.drop(columns=['calories', 'id'])
print(df)
## output
duration
0 50
1 40
2 45
You can also mention a list of column names to get the above output.
df = df.drop(['calories', 'id'])
print(df)
Instead of mentioning column names, you can also specify their index starting with 0 for first column, 1 for second column and so on. For this purpose, we list the indexes in dataframe.columns[]. Here is the command to delete ‘duration’ and ‘id’ columns.
df = df.drop(df.columns[[1, 2]], axis=1)
## output
calories
0 420
1 380
2 390
As you can see, using drop() function allows you to precisely remove one or more columns in pandas dataframe. Since it is so comprehensive, it has a more complicated syntax, as compared to using del command.
3. Using pop
You can also use pop command to remove a column from dataframe. Just mention the column name in dataframe.pop() function, as shown below. Here is an example to remove ‘duration’ column.
df.pop('duration')
print(df)
## output
calories id
0 420 1
1 380 2
2 390 3
Conclusion
In this article, we have learnt several simple ways to delete one or more columns in Pandas dataframe. If you want to delete just one column, then you can use del command. If you want to delete multiple columns, then use drop() function since it provides many different ways to remove multiple columns.
Also read:
How to Sort Python Dictionary by Value
How to Rename Columns in Pandas
How to Select Rows from Dataframe Based on Column Values

Sreeram Sreenivasan is the Founder of Ubiq. He has helped many Fortune 500 companies in the areas of BI & software development.