pandas
python
data manipulation
data analysis
dataframe

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.

python
1import pandas as pd
2
3df = pd.DataFrame({
4    "name": ["Lina", "Mark", "Ava"],
5    "age": [29, 35, 41],
6    "city": ["Toronto", "Paris", "Seoul"],
7})
8
9result = df.drop(columns=["age"])
10print(result)

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.

python
1import pandas as pd
2
3df = pd.DataFrame({
4    "name": ["Lina", "Mark", "Ava"],
5    "age": [29, 35, 41],
6    "city": ["Toronto", "Paris", "Seoul"],
7})
8
9excluded = "age"
10result = df.loc[:, df.columns != excluded]
11print(result)

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.

python
1import pandas as pd
2
3df = pd.DataFrame({
4    "name": ["Lina", "Mark", "Ava"],
5    "age": [29, 35, 41],
6    "city": ["Toronto", "Paris", "Seoul"],
7    "internal_id": [101, 102, 103],
8})
9
10kept_columns = [c for c in df.columns if not c.endswith("_id")]
11result = df[kept_columns]
12print(result)

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.

python
1import pandas as pd
2
3df = pd.DataFrame({
4    "feature1": [1.0, 2.0, 3.0],
5    "feature2": [10, 20, 30],
6    "target": [0, 1, 0],
7    "row_id": [11, 12, 13],
8})
9
10features = df.drop(columns=["target", "row_id"])
11print(features)

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.

python
1import pandas as pd
2
3df = pd.DataFrame({
4    "height": [165, 172, 180],
5    "weight": [60, 75, 82],
6    "class_label": [0, 1, 1],
7})
8
9X = df.drop(columns=["class_label"])
10y = df["class_label"]
11
12print(X)
13print(y)

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.

python
1import pandas as pd
2
3df = pd.DataFrame({"name": ["Lina"], "age": [29]})
4
5try:
6    df.drop(columns=["salary"])
7except KeyError as err:
8    print("Column problem:", err)

If you truly expect the column may be absent, you can opt into tolerant behavior.

python
safe_result = df.drop(columns=["salary"], errors="ignore")
print(safe_result)

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.columns are useful when exclusion rules are dynamic.
  • List comprehensions help when selection logic depends on naming patterns or configuration.
  • Default KeyError behavior is often helpful because it catches schema mistakes.
  • This pattern is especially common when separating feature columns from labels.

Course illustration
Course illustration

All Rights Reserved.