pandas
DataFrame
drop columns
data manipulation
Python

Drop columns whose name contains a specific string from pandas DataFrame

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

Dropping columns by name pattern is a common cleanup step in pandas workflows, especially after one-hot encoding, merged exports, or machine-generated feature sets. The main challenge is being precise enough to remove the intended columns without accidentally deleting similarly named data. A good solution starts by selecting matching column names explicitly, then dropping them in one clear operation.

Select Matching Columns Before Dropping

The safest pattern is to compute the columns you want to remove first. That makes the code easier to inspect and debug.

python
1import pandas as pd
2
3df = pd.DataFrame(
4    {
5        "id": [1, 2],
6        "temp_score": [10, 20],
7        "name": ["A", "B"],
8        "temp_flag": [True, False],
9    }
10)
11
12to_drop = [col for col in df.columns if "temp" in col]
13result = df.drop(columns=to_drop)
14
15print(to_drop)
16print(result)

This approach is simple, readable, and works well when matching logic is straightforward.

Use String Methods on the Columns Index

Pandas exposes vectorized string operations on df.columns, which makes pattern matching concise.

python
1mask = df.columns.str.contains("temp", regex=False)
2result = df.loc[:, ~mask]
3
4print(mask)
5print(result)

Using regex=False is a good default when you want literal substring matching. It avoids surprises from regular expression metacharacters.

This form is especially convenient when you want to keep non-matching columns rather than build a drop list separately.

It also scales well when the DataFrame has many columns because the intent stays explicit: compute a boolean mask, then keep only the inverse of that mask.

Case-Insensitive Matching

Real datasets often contain inconsistent casing such as TempValue, temp_value, or TEMP_COL. Normalize casing before matching if needed.

python
1mask = df.columns.str.lower().str.contains("temp", regex=False)
2result = df.loc[:, ~mask]
3
4print(result.columns.tolist())

This is safer than assuming naming conventions were applied consistently in upstream systems.

Regex Matching for More Control

When the rule is more specific than a plain substring, use a regex. For example, remove columns starting with tmp_.

python
1df2 = pd.DataFrame(
2    {
3        "tmp_value": [1, 2],
4        "tmp_flag": [0, 1],
5        "value": [10, 20],
6    }
7)
8
9mask = df2.columns.str.contains(r"^tmp_", regex=True)
10result = df2.loc[:, ~mask]
11
12print(result)

Regex is powerful, but use it only when the rule needs that expressiveness. Literal substring matching is easier to maintain.

Drop In Place or Return a New Frame

Most pandas methods return a new DataFrame unless you explicitly mutate. In data pipelines, returning a new frame is usually easier to reason about.

python
cleaned = df.drop(columns=[col for col in df.columns if "temp" in col])

If mutation is truly what you want:

python
df.drop(columns=[col for col in df.columns if "temp" in col], inplace=True)

Use inplace=True sparingly. It can make debugging and test setup harder because the original frame is no longer available for comparison.

Defensive Programming for Production Pipelines

In production code, add a small validation step before dropping columns so unexpected matches do not silently remove important data.

python
1to_drop = [col for col in df.columns if "temp" in col]
2
3print("Dropping columns:", to_drop)
4
5if not to_drop:
6    print("No matching columns found")
7
8result = df.drop(columns=to_drop)

For important datasets, log both the matched columns and the remaining schema. That makes debugging upstream naming changes much easier.

Alternative: Keep Only Wanted Columns

Sometimes it is safer to define the columns you want to keep instead of pattern-dropping unknown columns. That is especially true in regulated or analytics-critical pipelines.

python
1keep = [col for col in df.columns if "temp" not in col]
2result = df[keep]
3
4print(result)

This is functionally similar, but it shifts the mental model from removal to whitelist selection. In some teams that is the more maintainable style.

Another pandas-specific variant uses filter to select the matching names first:

python
1matched = df.filter(like="temp").columns
2result = df.drop(columns=matched)
3
4print(list(matched))
5print(result)

This is handy when you want pandas to do the name matching rather than writing the list comprehension yourself.

Common Pitfalls

  • Forgetting regex=False and accidentally treating the search string as a regular expression.
  • Using broad substrings and dropping columns that only partially match by coincidence.
  • Modifying the original DataFrame in place when downstream code still expects old columns.
  • Assuming column casing is consistent across all data sources.
  • Dropping columns without logging what matched, which makes schema drift harder to debug.

Summary

  • Compute matching column names explicitly before dropping them.
  • Use df.columns.str.contains for concise, vectorized matching.
  • Prefer literal substring matching unless regex is truly required.
  • Handle case sensitivity intentionally.
  • Return a new DataFrame by default and use in-place mutation only when there is a clear reason.

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.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.