Data Manipulation
Data Frames
Programming
Data Cleaning
Python Coding

Drop data frame columns by name

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Dropping DataFrame columns by name is one of the most common pandas cleanup tasks. It looks simple, but real pipelines still get tripped up by missing-column errors, hidden in-place mutations, and schema drift between environments.

The safest approach is to be explicit about which columns are being removed, whether missing columns should fail the job, and whether the transformation should return a new DataFrame or mutate the existing one.

The Standard Way: drop(columns=...)

The clearest API is DataFrame.drop with the columns argument.

python
1import pandas as pd
2
3df = pd.DataFrame(
4    {
5        "name": ["Alice", "Bob", "Carla"],
6        "age": [25, 30, 35],
7        "city": ["Toronto", "Berlin", "Lima"],
8    }
9)
10
11result = df.drop(columns=["age"])
12print(result)

Using columns= is usually more readable than relying on axis=1, especially when someone else reads the code later.

Dropping multiple columns is just as straightforward.

python
clean = df.drop(columns=["age", "city"])
print(clean)

Strict Versus Permissive Behavior

By default, pandas raises KeyError if any named column is missing.

python
# Raises KeyError if "unknown" is not present
strict = df.drop(columns=["unknown"])

That is often the right behavior in production because it catches upstream schema changes early.

If the input schema is intentionally variable, use errors="ignore".

python
safe = df.drop(columns=["unknown"], errors="ignore")
print(safe.columns.tolist())

This is useful in notebooks, ad hoc cleanup scripts, or pipelines where optional columns appear only in some feeds. The key point is to choose this behavior intentionally, not by habit.

In-Place or Return a New DataFrame

Pandas supports both styles.

python
1# Return a new DataFrame
2clean_df = df.drop(columns=["age"])
3
4# Mutate the existing DataFrame
5copy_df = df.copy()
6copy_df.drop(columns=["age"], inplace=True)

Many teams prefer the first style because it makes data flow easier to reason about and reduces accidental side effects. Functional-style transformations also chain better and are easier to test.

Dynamic Column Removal

Sometimes the columns are not known in advance. You may want to drop by prefix, suffix, or statistical rule.

Drop by name pattern:

python
1wide = pd.DataFrame(
2    {
3        "user_id": [1, 2],
4        "tmp_score": [0.1, 0.2],
5        "tmp_flag": [True, False],
6        "country": ["CA", "DE"],
7    }
8)
9
10cols_to_drop = [c for c in wide.columns if c.startswith("tmp_")]
11wide_clean = wide.drop(columns=cols_to_drop)
12print(wide_clean)

Drop columns with too many missing values:

python
missing_ratio = df.isna().mean()
cols_to_drop = missing_ratio[missing_ratio > 0.5].index
trimmed = df.drop(columns=cols_to_drop)

That kind of logic is common in ETL and feature-engineering pipelines.

Sometimes a Keep-List Is Better

If the input schema is noisy or unstable, selecting the columns you want can be safer than dropping columns you do not want.

python
required = ["name", "city"]
selected = df.loc[:, required]
print(selected)

A keep-list protects you from unexpected new columns entering the pipeline. In many production datasets, that is actually more robust than maintaining a drop-list forever.

Schema Validation After Dropping

After a schema-changing step, it is good practice to assert the result.

python
expected = {"name", "city"}
actual = set(result.columns)
assert actual == expected, f"Unexpected schema: {actual}"

This is especially helpful in jobs that run unattended. The earlier you detect an unexpected schema change, the easier it is to diagnose.

Common Pitfalls

A common mistake is using inplace=True everywhere and then losing track of where the DataFrame changed. That makes debugging transformation order harder than it needs to be.

Another issue is swallowing missing-column problems with errors="ignore" even when the schema should be fixed. That can hide real upstream data problems.

Developers also sometimes keep using axis=1 out of habit even though columns= is clearer and less error-prone.

Finally, do not forget that dropping columns changes downstream expectations. If later code still tries to access a removed column, the failure may show up far away from the original transformation.

Summary

  • Use df.drop(columns=[...]) to remove columns by name clearly.
  • Decide deliberately whether missing columns should raise or be ignored.
  • Prefer explicit assignment over heavy in-place mutation for maintainability.
  • Use pattern-based or rule-based drops when schemas are dynamic.
  • Consider a keep-list and schema assertions in production pipelines.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.