How to select all columns except one in pandas?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Selecting every DataFrame column except one is a routine pandas task. It comes up when you want to remove an identifier, exclude a target column before training a model, or keep most of a table unchanged while dropping one field. The cleanest solution is usually simple, but there are a few patterns worth knowing because they behave differently when column names are dynamic.
Use drop For The Most Readable Code
In most codebases, drop is the best default because it says exactly what you mean: remove these columns and keep the rest.
The result still contains name and city. This style is easy to scan in a review because the excluded column is explicit.
Use Column Filtering When The Rule Is Dynamic
Sometimes the column to exclude is computed at runtime. In those cases, filtering df.columns can be a better fit.
This pattern is compact and works well when you are already building a boolean mask for columns.
List Comprehension Gives Full Control
If the selection rule is more complex than "drop one known column," a list comprehension can be the clearest option.
This is useful when exclusion is based on a naming rule, a configuration list, or column metadata.
Excluding Several Columns
If you need every column except a small set, drop remains straightforward.
That is usually better than manually enumerating every column you want to keep, especially when new columns may be added later.
A Common Machine Learning Example
This question appears frequently when separating features from labels.
Using drop here avoids accidental label leakage into the feature matrix. The intent is obvious: everything is a feature except the target column.
What Happens If The Column Is Missing
By default, drop raises KeyError if the column does not exist. That default is often helpful because it exposes misspellings and unexpected schema changes.
If you truly expect the column may be absent, you can opt into tolerant behavior.
Use errors="ignore" deliberately. It is convenient for optional columns, but it can also hide bugs.
When To Prefer Each Pattern
There is no single mandatory idiom, but some choices are easier to maintain than others.
- Use
drop(columns=[...])when you know the excluded columns in advance. - Use
df.loc[:, mask]when you are already building a mask over column names. - Use list comprehension when the keep or exclude rule is custom and easier to express in Python logic.
In practice, drop is usually the most readable answer for "all columns except one."
Common Pitfalls
- Calling
drop("age")without specifying that you mean columns, which can be confusing in code review. - Using
errors="ignore"by default and hiding misspelled column names. - Manually listing many kept columns when excluding one or two columns would be clearer.
- Forgetting to exclude identifier or target columns when preparing model features.
- Building a dynamic mask that changes column order unexpectedly.
Summary
- '
df.drop(columns=["col"])is the clearest way to keep all columns except one in most cases.' - Boolean masks over
df.columnsare useful when exclusion rules are dynamic. - List comprehensions help when selection logic depends on naming patterns or configuration.
- Default
KeyErrorbehavior is often helpful because it catches schema mistakes. - This pattern is especially common when separating feature columns from labels.

