How to delete a column in pandas

In this Python tutorial, we will learn various methods for deleting a column in Python pandas. We’ll use some built-in functions to understand various approaches to deleting a column in Python Pandas

As a Developer, while making the Python Project I got the requirement to delete a column in pandas

Here we will see:

  • How to delete a column in pandas using a dataframe.pop()
  • How to delete a column in pandas using a del keyword
  • How to delete a column in Python pandas using drop()
  • How to delete multiple columns in Python pandas using drop()

Delete a column in Python pandas

In Python, there are primarily Six methods that are commonly used and important to understand when deleting a column in Python Pandas.

How to delete a column in pandas using a dataframe.pop()

  • In this section, we will discuss how to delete a column in the Python pandas using dataframe.pop().
  • By simply passing the name of the column as a parameter, we can use the pandas.dataframe.pop() method to remove or delete a column from a data frame.
  • This function returns the specified item and drops the specified item in the DataFrame. This technique enables us to remove any piece from the dataframe.

Syntax:

Here is the Syntax of pandas dataframe.pop() in Python

DataFrame.pop(item)

Note: This parameter takes only one parameter and it indicates the label for the following column that will be popped.

Example:

Let’s take an example and check how to delete a column in the Python pandas using dataframe.pop().

Source Code:

import pandas as pd 
new_info = {"Student_id": [672,378,782,125], "Student_Age":[13,18,12,16], "Student_name":['James','Potter','William','George']}
input = pd.DataFrame(new_info)
print("Input Data frame:\n", input)
input.pop("Student_Age")
print("\nData frame after deleting the column 'Student_Age':\n", input)

In the following given code first, we declared a dictionary and then used the pd.dataframe() function to convert them into a dataframe. Next, we removed the ‘student_age’ column from the dataframe by using input.pop() function.

Here is the Screenshot of the following given code.

How to delete a column in pandas using a dataframe.pop_
How to delete a column in pandas using a dataframe.pop()

This is how to delete a column in pandas using a dataframe.pop().

Read: Check If DataFrame is Empty in Python Pandas

How to delete a column in pandas using a del keyword

  • Now let us discuss how to delete a column in Python Pandas using a del keyword.
  • The del keyword in Python can also be used to remove a column direct from a data frame. In Python, the del keyword is typically used to remove or empty out objects.
  • In this example, we’ll generate a DataFrame and then use the del keyword to remove a particular column. Using the column label, the column is chosen for deletion.

Syntax:

Let’s have a look at the syntax and understand the working of the del keyword in Python

del dataframe['item']

Note: This parameter takes only one parameter and it indicates the label for the following column that will be deleted.

Example:

Here we will take an example and check how to delete a column in Python Pandas using a del keyword.

Source Code:

import pandas as pd

Country_info = {'State_names': ['Alabama', 'Alaska', 'Arizona'],
	'Zip_code': [8923,1234,78234]}

#create dataframe
new_val = pd.DataFrame(Country_info)
print("Input DataFrame: ",new_val)

#delete a column
del new_val['Zip_code']

print("After deleting the column :",new_val)

In the above code first, we imported the Pandas library and then declared a dictionary ‘Country_info’. Next, we converted the dictionary into a dataframe by using the pd.dataframe() function.

After that, we used the del keyword to delete the specific column from the dataframe.

You can refer to the below Screenshot

How to delete a column in pandas using a del keyword
How to delete a column in pandas using a del keyword

In this example, we have understood how to delete a column in pandas using a del keyword.

Read: Add row to Dataframe Python Pandas

How to delete a column in Python pandas using drop()

  • In this section, we will discuss how to delete a column in Python pandas using drop().
  • With the help of the pandas.dataframe.drop() function, we can remove values from a data frame. Either the values are row- or column-oriented.
  • To remove a particular column in this example’s dataframe, use the drop() function. To choose a column for deletion, we use the column label.

Syntax:

Here is the Syntax of pandas.dataframe.drop() in Python

dataframe.drop('item', inplace=True, axis=1)
  • It consists of a few parameters
    • item: it indicates the label for the following column that will be deleted.
    • inplace: Returns a duplicate of the DataFrame by default, False. When used True, it removes the current DataFrame’s column in place and returns None.
    • Axis: By default, it takes 1 which means we have to delete it for column-wise element. If it is 0 then it will delete it row-wise.

Example:

import pandas as pd 
emp_info = {"emp_id": [845,905,198,289], "emp_Age":[16,17,14,18], "emp_name":['Micheal','Robert','William','George']}
new_input = pd.DataFrame(emp_info)
print("Input Data frame:\n", new_input)
new_input.drop("emp_name", inplace=True, axis=1)
print("\nData frame after deleting the column 'emp_name':\n", new_input)

In the following given code first, we imported the Pandas library and then created a dataframe by using the pd.dataframe() function, and within this function, we assigned the input dictionary as an argument.

After that, we used the dataframe.drop() function and within this function, we mentioned the column name which we want to delete from the dataframe.

Here is the implementation of the following given code.

How to delete a column in Python pandas using drop
How to delete a column in Python pandas using a drop

Read: Python Pandas Drop Rows Example

How to delete multiple columns in Python pandas using drop()

  • Now let us understand how to delete multiple columns in Python pandas using drop().
  • With the help of the pandas.dataframe.drop() function, we can remove values from a data frame. Either the values are row- or column-oriented.
  • To remove numerous columns in the dataframe, use the drop() function. To choose which columns to delete, we use an array of column labels.

Example:

Let’s take an example and check how to delete multiple columns in Python pandas using drop().

Source Code:

import pandas as pd 
new_info = {"Student_id": [672,378,782,125], "Student_Age":[13,18,12,16], "Student_name":['James','Potter','William','George']}
input = pd.DataFrame(new_info)
print("Input Data frame:\n", input)
result=input.drop(['Student_Age', 'Student_name'], axis=1)
print("\nData frame after deleting the multiple columns :\n", result)

In the above code, we have to delete multiple columns from the dataframe for this we used the dataframe.drop() function. And within this function, we assigned multiple columns [‘Student_Age’, ‘Student_name’] along with that we assigned the axis=1 which means it will delete the elements column-wise.

Here is the Screenshot of the following given code.

How to delete multiple columns in Python pandas using drop
How to delete multiple columns in Python pandas using a drop()

As you can see in the Screenshot we have discussed How to delete multiple columns in Python pandas using a drop().

You may also like to read the following Python Pandas tutorials.

In this article, we have discussed various methods for deleting a column in Python pandas. Also, we have covered the following given topics.

  • How to delete a column in pandas using a dataframe.pop()
  • How to delete a column in pandas using a del keyword
  • How to delete a column in Python pandas using drop()
  • How to delete multiple columns in Python pandas using drop()