3 Ways to Remove a Column From a Python Dataframe

Filed Under: Python Advanced
Remove A Column From A Python Dataframe

Hello, readers! In this article, we will be focusing on 3 Ways to Remove a Column from a Python DataFrame in detail.

So, let us begin! 馃檪


Python Dataframe – Crisp Overview

Python offers us various data structures to deal with the data and perform operations on it. Especially when we think in the direction of data science and analysis, the data structures offered by Python have given a shape to the processing of it.

DataFrame is one such data structure offered by Python. It stores the data in the form of rows and columns. Now, this schema opens the door for having the datasets in place for analysis within the environment. These rows and columns are in sync are open for data preprocessing and manipulations.

Today we will discuss the ways to delete a column from a Dataframe. This scenario arises when we import a dataset into the Python environment and then while processing we get to know certain columns that are irrelevant to our modeling.

  1. pop() function
  2. drop() function
  3. del keyword

In real life, a Dataframe is equivalent to an excel sheet.


Method 1 – The pop() function

Python Dataframe provides us with the pop() function that enables the deletion of any column by accepting its name as a parameter.

Syntax:

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

Example:

Here, at first, we have created a python dataframe using DataFrame() function. Further, we make use of pop() function to delete the NAME column.

import pandas as pd 
data = {"Roll-num": [1,2,3,4], "Age":[12,14,13,15], "NAME":['X','Y','Z','A']}
frame= pd.DataFrame(data)
print("Data frame:\n")
print(frame)
frame.pop('NAME')
print("\nData frame post deleting the column 'NAME':\n")
print(frame)

Output:

Original Data frame:
 
   Roll-num  Age   NAME
0        1   12    X
1        2   14    Y
2        3   13    Z
3        4   15    A

 
Data frame after deleting the column 'NAME':
 
    Roll-num  Age   
0        1   12    
1        2   14    
2        3   13    
3        4   15    

Method 2 – The del keyword

Another technique to directly delete a data frame column is by using the del keyword. With the del keyword, we can entirely remove the data or the column from the dataset. With this, we can easily delete the column by specifying the column name.

Let us have a look at the below syntax!

Syntax:

del dataframe['column-name']

Example:

import pandas as pd 
data = {"Roll-num": [1,2,3,4], "Age":[12,14,13,15], "NAME":['X','Y','Z','A']}
frame= pd.DataFrame(data)
print("Data frame:\n")
print(frame)

del frame["NAME"]

print("\nData frame post deleting the column 'NAME':\n")
print(frame)

Output–

Original Data frame:
 
   Roll-num  Age   NAME
0        1   12    X
1        2   14    Y
2        3   13    Z
3        4   15    A

 
Data frame after deleting the column 'NAME':
 
    Roll-num  Age   
0        1   12    
1        2   14    
2        3   13    
3        4   15  

Method 3 – Python drop() method

Python drop() function offers us a higher level of customization when it comes to deleting columns from a data frame. This operation can be customized to be a row-oriented or a column-oriented operation.

Syntax:

dataframe.drop('column-name', inplace=True, axis=1)

Here, we specify the column that is to be deleted. Further, the inplace parameter if set to TRUE, stores the changes in the data frame post deletion into a new object and does not change or alter the original data frame object at all.

When the axis is set to 1, column-wise operations take place. When set to 0, row-wise operations occur.

Example:

import pandas as pd 
data = {"Roll-num": [1,2,3,4], "Age":[12,14,13,15], "NAME":['X','Y','Z','A']}
frame= pd.DataFrame(data)
print("Data frame:\n")
print(frame)
frame.drop('NAME', inplace=True, axis=1)
print("\nData frame post deleting the column 'NAME':\n")
print(frame)

Here, we have made use of drop() function to delete the column NAME by specifying axis = 1.

Output:

Original Data frame:
 
   Roll-num  Age   NAME
0        1   12    X
1        2   14    Y
2        3   13    Z
3        4   15    A

 
Data frame after deleting the column 'NAME':
 
    Roll-num  Age   
0        1   12    
1        2   14    
2        3   13    
3        4   15  

Conclusion

By this, we have come to the end of this topic. Feel free to comment below, in case you come across any questions. For more such posts related to Python programming, stay tuned with us.

Till then, Happy Learning!! 馃檪

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content