Pandas DataFrame – Delete Column
You can delete a column from a Pandas DataFrame with the del statement, the drop() method, or the pop() method. The best choice depends on whether you need a new DataFrame, want to modify the existing DataFrame, or need to keep the removed column.
- Use del to remove one column from the existing DataFrame.
- Use drop() to remove one or more columns, with the option to return a new DataFrame or modify the original.
- Use pop() to remove one column and return it as a Pandas Series.
Delete DataFrame Column using del keyword
To delete a column of DataFrame using del keyword, use the following syntax.
del myDataFrame['column_name']
In the following example, we shall initialize a DataFrame with three columns and delete one of the column using del keyword.
Python Program
import pandas as pd
#initialize a dataframe
df = pd.DataFrame({
'a':[14, 52, 46],
'b':[32, 85, 64],
'c':[88, 47, 36]})
#delete column 'b'
del df['b']
#print the dataframe
print(df)
Output
a c
0 14 88
1 52 47
2 46 36
The column with name b has been deleted from the dataframe. The del statement changes df directly and does not return the deleted values.
Delete DataFrame Column using drop() method
pandas.DataFrame.drop() method returns a new DataFrame with the specified columns dropped from the original DataFrame. The original DataFrame is not modified.
Python Program
import pandas as pd
#initialize a dataframe
df = pd.DataFrame({
'a':[14, 52, 46],
'b':[32, 85, 64],
'c':[88, 47, 36]})
#delete column 'b'
df1 = df.drop(['b'], axis=1)
#print the dataframe
print(df1)
Output
a c
0 14 88
1 52 47
2 46 36
Here, axis=1 tells Pandas to remove a column rather than a row. The clearer equivalent is df.drop(columns=['b']).
Remove Multiple Pandas DataFrame Columns with drop()
Pass a list of column labels to the columns parameter when several columns must be removed at once.
import pandas as pd
df = pd.DataFrame({
'name': ['Asha', 'Ravi', 'Mina'],
'age': [24, 31, 28],
'city': ['Pune', 'Delhi', 'Chennai'],
'score': [82, 91, 88]
})
result = df.drop(columns=['age', 'score'])
print(result)
Output
name city
0 Asha Pune
1 Ravi Delhi
2 Mina Chennai
The original df still contains all four columns because the returned DataFrame was assigned to result.
Delete a Pandas Column In Place with drop()
Set inplace=True when the existing DataFrame should be changed directly. In this form, drop() returns None.
df.drop(columns=['city'], inplace=True)
print(df)
Alternatively, assign the result back to the same variable:
df = df.drop(columns=['city'])
Reassignment is often easier to follow in a sequence of DataFrame transformations.
Delete DataFrame Column using pop() method
pandas.DataFrame.pop() method deletes specified column from the DataFrame and returns the deleted column.
Python Program
import pandas as pd
#initialize a dataframe
df = pd.DataFrame({
'a':[14, 52, 46],
'b':[32, 85, 64],
'c':[88, 47, 36]})
#delete column 'b'
poppedColumn = df.pop('b')
#print the dataframe
print(df)
print('\nDeleted Column\n-------------')
#print deleted column
print(poppedColumn)
Output
a c
0 14 88
1 52 47
2 46 36
Deleted Column
-------------
0 32
1 85
2 64
Name: b, dtype: int64
Use pop() when the removed column is still needed for another calculation. It accepts one column label at a time.
Handle a Missing DataFrame Column During Deletion
del, pop(), and drop() normally raise a KeyError when the requested column does not exist. With drop(), use errors='ignore' when a missing label should be skipped.
df = df.drop(columns=['temporary_column'], errors='ignore')
Use this option only when ignoring an absent column is intentional. Otherwise, allowing the error can reveal a misspelled or unexpected column name.
Choose Between del, drop(), and pop() for Pandas Columns
| Method | Changes original DataFrame | Removes multiple columns | Returns removed column |
|---|---|---|---|
del df['column'] | Yes | No | No |
df.drop(columns=[...]) | Only with inplace=True or reassignment | Yes | No |
df.pop('column') | Yes | No | Yes, as a Series |
For most column-removal tasks, drop(columns=[...]) is the most explicit and flexible form. Use del for a simple direct deletion and pop() when the deleted values must be retained.
Frequently Asked Questions About Deleting Pandas Columns
How do I delete a Pandas DataFrame column by name?
Use df.drop(columns=['column_name']) to return a DataFrame without that column. Use del df['column_name'] to modify the current DataFrame directly.
How do I delete several columns from a Pandas DataFrame?
Pass all required labels in a list: df.drop(columns=['column_a', 'column_b']).
Does DataFrame.drop() change the original DataFrame?
Not by default. Save the returned DataFrame, assign it back to the same variable, or specify inplace=True.
How do I delete a column only when it exists?
Use df.drop(columns=['column_name'], errors='ignore'), or check if 'column_name' in df.columns before deleting it.
Summary of Pandas DataFrame Column Deletion
In this Pandas Tutorial, we learned how to delete a column from DataFrame using del, drop(), and pop(). We also removed multiple columns, updated a DataFrame in place, and handled missing column labels.
TutorialKart.com