Skip to main content

How to Drop a Column from a pandas DataFrame

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Most tables carry columns you don't need: notes, IDs, or values a model mustn't see. frame.drop(columns=...) removes them and returns a new table, leaving the original untouched. This guide drops one column, then two, then shows what happens when the name doesn't exist, with real output from pandas 3.1.1.

Syntax​

Syntax
smaller = frame.drop(columns="column")
smaller = frame.drop(columns=["column_1", "column_2"])
PartWhat it means
columns="column"The column to remove. A list removes several at once
smaller = ...drop returns a new DataFrame. The original keeps every column
errors="ignore" (optional)Don't complain about names that aren't in the table

Step 1: Drop One Column, Then Two​

The example is a small table of phone calls. Create drop_column.py:

The drop_column.py script in a macOS-style code window: it builds a calls DataFrame with customer, minutes, notes and signed_up columns, prints it after drop columns minutes, then after dropping minutes and notes, then prints the original's column names.

drop_column.py
import pandas as pd

calls = pd.DataFrame(
{"customer": ["ana", "ben"], "minutes": [4.5, 1.0], "notes": ["call back", ""], "signed_up": [1, 0]}
)

print(calls.drop(columns="minutes"))
print(calls.drop(columns=["minutes", "notes"]))
print(calls.columns.tolist())
Terminal
uv run python drop_column.py
Output
customer notes signed_up
0 ana call back 1
1 ben 0
customer signed_up
0 ana 1
1 ben 0
['customer', 'minutes', 'notes', 'signed_up']

A terminal running uv run python drop_column.py: the table without minutes, then the table with only customer and signed_up, then the original's four column names, minutes and notes included.

The last line shows that calls itself still has all four columns: each drop returned a new table. To keep the result, assign it: calls = calls.drop(columns="minutes").

Step 2: Drop a Column That Isn't There​

Create drop_missing.py, which drops a notes column from a table that has none, first with errors="ignore", then without:

drop_missing.py
import pandas as pd

calls = pd.DataFrame({"customer": ["ana", "ben"], "minutes": [4.5, 1.0]})

print(calls.drop(columns="notes", errors="ignore").columns.tolist())
calls.drop(columns="notes")
Terminal
uv run python drop_missing.py
Output (traceback shortened)
['customer', 'minutes']
Traceback (most recent call last):
File "/Users/you/source/python-demo/drop_missing.py", line 6, in <module>
calls.drop(columns="notes")
...
KeyError: "['notes'] not found in axis"

A terminal running uv run python drop_missing.py: with errors ignore, the columns customer and minutes are printed; without it, a traceback ending in KeyError: ['notes'] not found in axis.

The KeyError is usually the help you want: it catches a misspelled name before the column you meant to drop slips through. Use errors="ignore" only when the column is genuinely optional.

Common Mistakes​

MistakeWhat happensDo this instead
Calling calls.drop(columns="minutes") without assigning the resultThe column is still therecalls = calls.drop(columns="minutes")
calls.drop("minutes") without columns=KeyError: "['minutes'] not found in axis": by default drop removes rowsName it: columns="minutes"
errors="ignore" by habitA misspelled column is silently keptLeave the default, so a typo raises an error

Troubleshooting​

SymptomLikely causeFix
KeyError: "['notes'] not found in axis"No column with that exact namePrint frame.columns.tolist() and copy the name
The column is still in the table after dropThe result wasn't assignedAssign it back

FAQ​

What about del frame["column"]?​

It also removes a column, but changes the table itself and takes one name at a time. drop works on copies, which is easier to follow when a table passes through several steps.

Can I keep a list of columns instead?​

Yes, choosing columns works the other way round: How to Choose the Columns of a pandas DataFrame with reindex.

Conclusion​

calls.drop(columns="minutes") and calls.drop(columns=["minutes", "notes"]) returned smaller tables and left calls as it was. Assign the result to keep it, name the columns with columns=, and let the KeyError catch misspelled names.

Additional Resources​