Text columns
Last updated
Store names, towns, product labels and comments are text, and text is where data is messiest: extra spaces, capitals, Sto. and Santo, ñ and n. In this lesson you'll clean and search text columns with the .str tools, pull numbers out of labels with a regular expression, and learn the one character that makes a search match far too much.
By the end of this lesson you can
- Clean text with
str.strip,str.titleandstr.replace - Search with
str.contains, and split text withstr.split - Pull part of a label out with
str.extractand a regular expression - Say when a pattern is a regular expression, and why
.matches anything
Cleaning and searching
Six town names, typed by six different people. Run the cells.
.str gives a whole column Python's string methods, from Simple Python, applied to every value:
str.strip()removes spaces at both ends;str.title()makes each word start with a capital.LIPA CITYandlas piñasnow match the others.str.replace("Sto. ", "Santo ", regex=False)turns the short form into the long one, so the two Santo Tomas rows become one town. Pick one spelling for each place and write the rule down: Sto. and Santo, Sta. and Santa, Gen. and General.str.contains("City")givesTrueorFalsefor each row: a mask for filtering.str.split(" ").str[0]splits each value into a list of words and takes the first.
Keep the ñ. Parañaque and Paranaque are both common in data, but the official name has ñ; change the plain-n spellings to it, not the other way round. Lesson 5.8 matches place names to official codes, which settles spelling for good.
Patterns
The last cell above pulls the size out of product labels like Rice 25 kg. Its pattern is a regular expression: a small language for describing text.
| pattern | matches |
|---|---|
\d | one digit |
\d+ | one or more digits |
. | any one character |
\. | a real dot |
( … ) | the part to keep |
So (\d+) kg means "some digits, a space and kg; keep the digits". str.extract returns what's in the brackets, for each row: 25, 1 and 5. Oil 2 L has no kg, so it gets NaN. The result is still text: add .astype("Int64") to make it numbers.
Write patterns as raw strings, r"...", so Python leaves the backslashes alone.
Wrong way: the dot that matches everything
You want the places written with the short form Sto.. Step through, then run the cells.
Sto..str.contains treats its pattern as a regular expression unless you say otherwise, and in a regular expression . means any character. So Sto. matches Stor in Store 5 and Ston in Stone Bridge. Three matches where you wanted one, and no error.
regex=False makes the dot a dot. The last cell shows the same trap in str.replace with regex=True: the pattern . matches every character, so every name is replaced with nothing.
pandas methods differ here: str.contains and str.extract default to regex, and str.replace doesn't. So when the text you're searching for has ., (, +, ? or $ in it, pass regex=False, or put a backslash before the character (Sto\.).
Check what you learned
Sources
Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.
- pandas: Working with text data: the .str methods, and regex in extract and contains
- The Python Standard Library: re, regular expression syntax: what ., \d, + and brackets mean
- Python HOWTOs: Regular Expression HOWTO: a gentle introduction to patterns