pandas
data cleaning
NaN
Python
data preprocessing

Replacing blank values white space with NaN in pandas

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In pandas, blank strings and whitespace-only values often look like data even though they really mean "missing". Replacing them with NaN or pd.NA is an important cleanup step because pandas can then treat those cells as missing values during filtering, aggregation, and type conversion.

Replace Empty and Whitespace-Only Strings

A practical pattern is to use a regular expression that matches cells containing only whitespace.

python
1import numpy as np
2import pandas as pd
3
4df = pd.DataFrame({
5    "name": ["Ana", "  ", "", "Ben"],
6    "city": ["Paris", "Rome", "   ", "Berlin"],
7})
8
9cleaned = df.replace(r"^\s*$", np.nan, regex=True)
10print(cleaned)

Output:

text
1   name    city
20   Ana   Paris
31   NaN    Rome
42   NaN     NaN
53   Ben  Berlin

The pattern ^\s*$ matches:

  • an empty string
  • a string made only of spaces
  • a string made only of tabs or other whitespace

That is usually the most direct answer to this problem.

Why replace("", np.nan) Is Not Enough

If you only do this:

python
df = df.replace("", np.nan)

you handle truly empty strings, but not cells containing " " or "\t". Many messy CSV and spreadsheet exports contain whitespace rather than empty strings, which is why the regex-based solution is more robust.

Clean Specific Columns Only

Sometimes you want to avoid touching every column. In that case, target only the text columns you care about:

python
text_columns = ["name", "city"]
df[text_columns] = df[text_columns].replace(r"^\s*$", np.nan, regex=True)

That is useful when other columns already contain structured values and you want to keep the cleanup scope explicit.

Strip First When the Problem Is Leading and Trailing Spaces

There is a difference between:

  • cells that are only whitespace
  • cells that contain real text plus unwanted outer spaces

If you want to preserve meaningful text while trimming around it, strip first:

python
df["name"] = df["name"].str.strip()
df["name"] = df["name"].replace("", np.nan)

This turns " Ana " into "Ana" and " " into missing data.

For a whole set of string columns:

python
1text_columns = ["name", "city"]
2
3for col in text_columns:
4    df[col] = df[col].str.strip().replace("", np.nan)

That approach is often better when the real task is both normalization and missing-value cleanup.

np.nan Versus pd.NA

Both can represent missing data, but they behave a little differently depending on dtype. If you are working heavily with pandas extension dtypes, pd.NA may fit better. For general cleanup, np.nan is still common and works fine in many workflows.

The important part is consistency. Once blanks become missing values, downstream operations such as isna(), dropna(), and type conversion become much easier to reason about.

A Typical Follow-Up Step

After replacement, you can inspect or filter missing rows:

python
missing_rows = cleaned[cleaned["name"].isna() | cleaned["city"].isna()]
print(missing_rows)

This is one reason the replacement matters so much. Blank strings do not automatically participate in pandas missing-value tools, but NaN does.

Common Pitfalls

  • Replacing only "" and forgetting about strings that contain spaces or tabs.
  • Applying string cleanup to non-string columns without checking their dtype first.
  • Forgetting the difference between trimming real text and marking whitespace-only cells as missing.
  • Replacing blanks with NaN and then wondering why numeric conversion changes behavior. Missing values affect dtype decisions.
  • Cleaning one DataFrame copy and accidentally continuing to analyze the uncleaned original.

Summary

  • In pandas, whitespace-only cells are often best treated as missing data.
  • 'df.replace(r"^\s*$", np.nan, regex=True) is a strong general solution.'
  • Use column-specific cleanup when you do not want to modify the whole DataFrame.
  • Strip text first if you also need to normalize leading and trailing spaces.
  • Converting blanks to NaN makes pandas missing-value operations work as intended.

Course illustration
Course illustration

All Rights Reserved.