If your are looking for the answer “Delete a column from a Pandas DataFrame”. So you are at the right place. This article covers the answer to your question. In this article we are going to cover different ways or methods to answer the question “Delete a column from a Pandas DataFrame”. Let’s see how to delete a column from a DataFrame in Pandas.
How to delete a column from a Pandas DataFrame
In pandas, there are numerous ways to delete a column from a DataFrame in Pandas, each with its own set of advantages and disadvantages.
Method 1: By using drop() method of the DataFrame
DataFrame has a method called drop() that removes rows or columns according to specify column(label) names and corresponding axis.
In order to delete columns, you need to specify axis=1 as shown below:
# Import pandas package
import pandas as pd
# Define a dictionary containing students information
data = {'Name': ['Rahul', 'Pooja',
'Ketan', 'Mili'],
'RollNo': [101, 102, 103, 104],
'Subject': ['Hindi', 'English',
'Math', 'Science'],
'Marks': [79, 85, 89, 95]}
# Convert the dictionary into DataFrame
df = pd.DataFrame(data, columns=['Name', 'RollNo',
'Subject', 'Marks'])
print("Given Dataframe :\n", df)
# Remove column name 'Name'
df = df.drop(['Name'], axis=1)
# After deletion
print("After Deletion Dataframe is :\n", df)
Output:
After Deletion Dataframe is :
RollNo Subject Marks
0 101 Hindi 79
1 102 English 85
2 103 Math 89
3 104 Science 95
Method 2: By using ‘del’ method of the DataFrame.
del is also an option, you can delete a column by del df['column name']. The Python would map this operation to df.__delitem__('column name'), which is an internal method of DataFrame.
# Import pandas package
import pandas as pd
# Define a dictionary containing students information
data = {'Name': ['Rahul', 'Pooja',
'Ketan', 'Mili'],
'RollNo': [101, 102, 103, 104],
'Subject': ['Hindi', 'English',
'Math', 'Science'],
'Marks': [79, 85, 89, 95]}
# Convert the dictionary into DataFrame
df = pd.DataFrame(data, columns=['Name', 'RollNo',
'Subject', 'Marks'])
print("Given Dataframe :\n", df)
# Remove column 'Marks'
del df['Marks']
# After deletion
print("After Deletion Dataframe is :\n", df)
Output:
After Deletion Dataframe is :
Name RollNo Subject
0 Rahul 101 Hindi
1 Pooja 102 English
2 Ketan 103 Math
3 Mili 104 Science
Method 3: By using pop() method of the DataFrame.
pop() function would also drop the column. Unlike the other two methods, this function would return the column.
# Import pandas package
import pandas as pd
# Define a dictionary containing students information
data = {'Name': ['Rahul', 'Pooja',
'Ketan', 'Mili'],
'RollNo': [101, 102, 103, 104],
'Subject': ['Hindi', 'English',
'Math', 'Science'],
'Marks': [79, 85, 89, 95]}
# Convert the dictionary into DataFrame
df = pd.DataFrame(data, columns=['Name', 'RollNo',
'Subject', 'Marks'])
print("Given Dataframe :\n", df)
# Remove column 'Marks'
df.pop('Marks')
# After deletion
print("After Deletion Dataframe is :\n", df)
Output:
After Deletion Dataframe is :
Name RollNo Subject
0 Rahul 101 Hindi
1 Pooja 102 English
2 Ketan 103 Math
3 Mili 104 Science
Final Thoughts
Deleting columns from pandas DataFrames is a fairly common task. In today’s article we have explored how you can delete a specific column by name. So far, we have discussed how to delete a column from a pandas DataFrame by using below methods:
delcommanddrop()methodpop()method