Reference · Python (pandas)
Turn blank text into missing in Python (pandas)
Convert empty text into a missing value so blanks and missing values are handled by one rule.
When to use it
After trimming, when the rules say an empty result means nothing was recorded.
How
Series.mask(s == "") replaces empty strings with <NA>.
k = df["customer_key"].str.strip(" \t\r\n")
df["customer_key"] = k.mask(k == "")- With the string dtype, the result keeps <NA> for missing.
Pitfalls
- Running it before trimming.
- Treating zero as blank.
Lesson: Missing is not empty (and neither is zero)
Practise it: Join orders to customers without matching blanks