Delete a column from a Pandas DataFrame
Categories:
How to Delete Columns from a Pandas DataFrame

Learn various methods to efficiently remove one or more columns from a Pandas DataFrame using drop()
, del
, and other techniques.
Pandas DataFrames are fundamental data structures for data manipulation in Python. Often, during data cleaning or feature engineering, you'll need to remove columns that are no longer relevant, contain redundant information, or are simply not needed for your analysis. This article explores the most common and efficient ways to delete columns from a Pandas DataFrame, covering single column deletion, multiple column deletion, and in-place modifications.
Understanding Column Deletion Methods
There are several primary methods to delete columns in Pandas, each with its own use case and implications for the original DataFrame. The choice of method often depends on whether you want to modify the DataFrame in place or return a new DataFrame with the specified columns removed.
flowchart TD A[Start] --> B{Choose Deletion Method} B --> C{Single Column?} C -->|Yes| D[Use `del` keyword] C -->|No| E{Multiple Columns?} E -->|Yes| F[Use `df.drop()`] E -->|No| G[End] D --> H{In-place?} F --> I{In-place?} H -->|Yes| J[Original DataFrame modified] H -->|No| K[Error: `del` is always in-place] I -->|Yes| J I -->|No| L[New DataFrame returned] J --> G K --> G L --> G
Decision flow for choosing a Pandas column deletion method.
Method 1: Using df.drop()
for Flexible Deletion
The df.drop()
method is the most versatile way to remove columns (and rows) from a DataFrame. It allows you to specify columns by their labels and control whether the operation modifies the DataFrame in place or returns a new DataFrame. This is generally the recommended method for most scenarios, especially when deleting multiple columns or when you need a new DataFrame without altering the original.
import pandas as pd
# Create a sample DataFrame
data = {
'col_A': [1, 2, 3],
'col_B': [4, 5, 6],
'col_C': [7, 8, 9],
'col_D': [10, 11, 12]
}
df = pd.DataFrame(data)
print("Original DataFrame:\n", df)
# 1. Delete a single column (returns a new DataFrame)
df_no_C = df.drop('col_C', axis=1)
print("\nDataFrame after dropping 'col_C' (new DataFrame):\n", df_no_C)
print("Original DataFrame remains unchanged:\n", df)
# 2. Delete multiple columns (returns a new DataFrame)
df_no_BD = df.drop(['col_B', 'col_D'], axis=1)
print("\nDataFrame after dropping 'col_B' and 'col_D':\n", df_no_BD)
# 3. Delete columns in-place (modifies the original DataFrame)
df.drop(['col_A', 'col_B'], axis=1, inplace=True)
print("\nOriginal DataFrame after in-place drop of 'col_A' and 'col_B':\n", df)
Examples of using df.drop()
to remove columns.
axis=1
when dropping columns. If axis
is omitted or set to 0
, Pandas will attempt to drop rows with the specified labels, which can lead to unexpected errors or behavior.Method 2: Using the del
Keyword
The del
keyword is a standard Python statement used to delete objects. When applied to a DataFrame column, it directly removes the column from the DataFrame. This method is concise and efficient for deleting a single column, but it always modifies the DataFrame in place and cannot be used for multiple columns simultaneously.
import pandas as pd
# Create a sample DataFrame
data = {
'col_X': [10, 20, 30],
'col_Y': [40, 50, 60],
'col_Z': [70, 80, 90]
}
df_del = pd.DataFrame(data)
print("Original DataFrame for `del`:\n", df_del)
# Delete 'col_Y' using del
del df_del['col_Y']
print("\nDataFrame after `del` 'col_Y':\n", df_del)
Deleting a single column using the del
keyword.
del
keyword modifies the DataFrame in place and does not return a new DataFrame. If you need to preserve the original DataFrame, make a copy first (e.g., df_copy = df.copy()
) before using del
.Method 3: Using df.pop()
for Single Column Deletion and Retrieval
The df.pop()
method removes a single column from the DataFrame and returns the removed Series. This can be useful if you need to delete a column and immediately use its data for further processing. Like del
, pop()
always modifies the DataFrame in place.
import pandas as pd
# Create a sample DataFrame
data = {
'col_P': [100, 200, 300],
'col_Q': [400, 500, 600],
'col_R': [700, 800, 900]
}
df_pop = pd.DataFrame(data)
print("Original DataFrame for `pop`:\n", df_pop)
# Pop 'col_Q'
popped_column = df_pop.pop('col_Q')
print("\nDataFrame after `pop` 'col_Q':\n", df_pop)
print("\nPopped column (Series):\n", popped_column)
Using df.pop()
to remove and retrieve a column.
df.pop()
is useful for single column removal where you also need the column's data, df.drop()
with inplace=True
is generally preferred for simple in-place deletion as it's more explicit about its purpose.