Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages
wb_sunny

3 Easy Ways to Remove a Column From a Python Dataframe

Delete A Column From A Dataframe

Hello, readers! In this article, we will be focusing on ways to remove a column from a Python dataframe. So, let us get started.


First, what is a DataFrame?

So, folks! Eventually before stepping towards the solution, it is very necessary for us to understand and recall the meaning and existence of a DataFrame.

A DataFrame is a data structure offered by the Python Pandas module. It stores values in the form of rows and columns. Thus, we can have the data in the form of a matrix representing the entities as rows and columns.

A DataFrame resembles an Excel or CSV file in the real world.


How to Remove a Column From a Python Dataframe?

So, having understood about a dataframe, let us now focus on the techniques to remove a column entirely from a DataFrame.

1. Python dataframe.pop() method

We can use pandas.dataframe.pop() method to remove or delete a column from a data frame by just providing the name of the column as an argument.

Syntax:

pandas.dataframe.pop('column-name')

Example:

import pandas as pd 
data = {"Roll-num": [10,20,30,40,50,60,70], "Age":[12,14,13,12,14,13,15], "NAME":['John','Camili','Rheana','Joseph','Amanti','Alexa','Siri']}
block = pd.DataFrame(data)
print("Original Data frame:\n")
print(block)
block.pop('NAME')
print("\nData frame after deleting the column 'NAME':\n")
print(block)

Here, we have created a Python dict as ‘data’ and further converted it into a data frame using pandas.DataFrame() method.

Further, we have applied pop() method to delete the column.

Output:

Original Data frame:

   Roll-num  Age    NAME
0        10   12    John
1        20   14  Camili
2        30   13  Rheana
3        40   12  Joseph
4        50   14  Amanti
5        60   13   Alexa
6        70   15    Siri

Data frame after deleting the column 'NAME':

   Roll-num  Age
0        10   12
1        20   14
2        30   13
3        40   12
4        50   14
5        60   13
6        70   15

2. Python del keyword to remove the column

Python del keyword can also be used to directly flush the column from the data frame. The del keyword is usually used to delete or flush out objects in Python.

Have a look at the below syntax!

Syntax:

del dataframe['column-name']

Example:

import pandas as pd 
data = {"Roll-num": [10,20,30,40,50,60,70], "Age":[12,14,13,12,14,13,15], "NAME":['John','Camili','Rheana','Joseph','Amanti','Alexa','Siri']}
block = pd.DataFrame(data)
print("Original Data frame:\n")
print(block)
del block["NAME"]
print("\nData frame after deleting the column 'NAME':\n")
print(block)

Output:

Original Data frame:

   Roll-num  Age    NAME
0        10   12    John
1        20   14  Camili
2        30   13  Rheana
3        40   12  Joseph
4        50   14  Amanti
5        60   13   Alexa
6        70   15    Siri

Data frame after deleting the column 'NAME':

   Roll-num  Age
0        10   12
1        20   14
2        30   13
3        40   12
4        50   14
5        60   13
6        70   15

3. Python drop() function to remove a column

The pandas.dataframe.drop() function enables us to drop values from a data frame. The values can either be row-oriented or column-oriented.

Have a look at the below syntax!

dataframe.drop('column-name', inplace=True, axis=1)
  • inplace: By setting it to TRUE, the changes gets stored into a new object that gets created and it does not alter the original dataframe.
  • axis: 1 is for column wise operations and 0 is for row wise operations.

Example:

import pandas as pd 
data = {"Roll-num": [10,20,30,40,50,60,70], "Age":[12,14,13,12,14,13,15], "NAME":['John','Camili','Rheana','Joseph','Amanti','Alexa','Siri']}
block = pd.DataFrame(data)
print("Original Data frame:\n")
print(block)
block.drop('NAME', inplace=True, axis=1)
print("\nData frame after deleting the column 'NAME':\n")
print(block)

Output:

Original Data frame:

   Roll-num  Age    NAME
0        10   12    John
1        20   14  Camili
2        30   13  Rheana
3        40   12  Joseph
4        50   14  Amanti
5        60   13   Alexa
6        70   15    Siri

Data frame after deleting the column 'NAME':

   Roll-num  Age
0        10   12
1        20   14
2        30   13
3        40   12
4        50   14
5        60   13
6        70   15

Conclusion

By this, we have come to the end of this article. Hope this article insights your interest for a better.

Feel free to comment below, in case you come across any question. Till then, Happy Learning!! 🙂


References