Reference · Python (pandas)
Trim whitespace in Python (pandas)
Remove a declared set of characters from both ends of a text value without touching the inside.
When to use it
Keys, codes and categories padded with spaces, tabs or line breaks, before any comparison or lookup.
How
Series.str.strip(chars) with the exact characters. Without an argument it removes every Unicode whitespace character.
df["customer_code"].str.strip(" \t\r\n")- Missing values stay <NA>.
- Use .mask(s == "") afterwards to turn empty strings into missing.
Pitfalls
- Relying on the tool's default definition of whitespace.
- Casting identifiers to numbers to drop padding.
- Trimming only one side.
Lesson: Trim the right characters, and only those
Practise it: Clean an order-line export