data cleaning
string manipulation
pandas
data preprocessing
python

Remove unwanted parts from strings in a column

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

Cleaning text columns is a routine part of data preparation. Real datasets often contain prefixes, suffixes, IDs, punctuation, or inconsistent spacing that make grouping and analysis harder than they need to be.

In pandas, the cleanest approach is usually to express the transformation at the column level instead of looping row by row. That keeps the code readable and uses pandas string methods that are already optimized for tabular data.

Start with a Concrete Example

Suppose a CSV file contains product descriptions with extra text you do not want to keep:

python
1import pandas as pd
2
3
4df = pd.DataFrame(
5    {
6        "raw_name": [
7            "SKU-101: Apple (fresh)",
8            "SKU-102: Banana (fresh)",
9            "SKU-103: Pear (fresh)",
10        ]
11    }
12)
13
14print(df)

If the goal is to keep only the product name, you need to remove the SKU prefix, the colon separator, and the trailing (fresh) marker.

Use str.replace for Pattern-Based Cleaning

For repeated patterns, Series.str.replace is often the most practical tool. It supports both literal replacement and regular expressions.

python
1import pandas as pd
2
3
4df = pd.DataFrame(
5    {
6        "raw_name": [
7            "SKU-101: Apple (fresh)",
8            "SKU-102: Banana (fresh)",
9            "SKU-103: Pear (fresh)",
10        ]
11    }
12)
13
14cleaned = (
15    df["raw_name"]
16    .str.replace(r"^SKU-\d+:\s*", "", regex=True)
17    .str.replace(r"\s*\(fresh\)$", "", regex=True)
18)
19
20print(cleaned)

This works well because the unwanted parts follow predictable patterns. The first regex removes the SKU prefix from the start of the string, and the second removes the trailing freshness marker from the end.

Use str.strip and Friends for Whitespace Cleanup

A lot of dirty string data is just messy spacing. Once the major patterns are removed, normalize whitespace too.

python
1df["name"] = (
2    df["raw_name"]
3    .str.replace(r"^SKU-\d+:\s*", "", regex=True)
4    .str.replace(r"\s*\(fresh\)$", "", regex=True)
5    .str.strip()
6)

If the strings contain repeated spaces in the middle, collapse them as well:

python
df["name"] = df["name"].str.replace(r"\s+", " ", regex=True)

That gives you consistent values for later joins, comparisons, and aggregation.

Removing Fixed Substrings Without Regex

If the unwanted text is always an exact literal string, you do not need regex. A literal replacement is simpler and often easier to maintain.

python
1import pandas as pd
2
3
4df = pd.DataFrame({"city": ["City: Toronto", "City: Ottawa"]})
5df["city"] = df["city"].str.replace("City: ", "", regex=False)
6
7print(df)

Use literal replacement when the data format is stable and you do not need pattern matching. Regex is powerful, but unnecessary regex can make a cleaning step harder to read than it needs to be.

Cleaning with a Custom Function

Sometimes the transformation is too specific for a single regex. In that case, write a small Python function and apply it explicitly.

python
1import pandas as pd
2
3
4def normalize_label(value: str) -> str:
5    value = value.replace("[TEMP]", "")
6    value = value.replace("_", " ")
7    value = " ".join(value.split())
8    return value.title()
9
10
11df = pd.DataFrame({"label": ["[TEMP]red_apple", "green_banana"]})
12df["label"] = df["label"].apply(normalize_label)
13
14print(df)

This is less vectorized than chaining str methods, but it is still a good option when the rule is business-specific and a custom function is more understandable than a dense regex.

Choosing the Right Tool

A reasonable rule of thumb is:

  • use str.replace(..., regex=False) for exact text removal
  • use str.replace(..., regex=True) for structural patterns
  • use str.strip() for leading and trailing whitespace
  • use .apply(...) only when the cleaning logic is too custom for the built-in string methods

That order keeps simple problems simple while still leaving room for more involved cleanup steps.

Common Pitfalls

One common mistake is forgetting that regex treats characters such as parentheses and dots specially. If you mean a literal ( or ., escape it or disable regex mode.

Another issue is cleaning NaN values as if they were ordinary strings. Pandas string methods usually preserve missing values, but custom Python functions may need explicit null handling.

It is also easy to over-clean and remove information you later need. Before replacing patterns across a whole column, inspect a few real examples and confirm the transformation on actual sample rows.

Finally, do not reach for a manual for loop first. Column-level string operations are usually clearer and more idiomatic in pandas.

Summary

  • Pandas string methods are the normal way to remove unwanted parts from a text column.
  • Use literal replacement for exact substrings and regex replacement for repeated patterns.
  • Follow structural cleanup with whitespace normalization such as str.strip().
  • Use a custom function only when the cleaning rule is too specific for built-in string helpers.
  • Test the transformation on representative sample values so you do not remove useful information by accident.

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.