pandas in depth · lesson 8 of 12
Parts in this lesson
As.str.strip().str.title()tidy spaces and capitals
Bs.str.contains("City")True where the text appears
Cs.str.extract(r"(\d+) kg")the number before kg

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.title and str.replace
  • Search with str.contains, and split text with str.split
  • Pull part of a label out with str.extract and 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.

towns.ipynb

.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 CITY and las piñas now 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") gives True or False for 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.

patternmatches
\done 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.

What “Sto.” matches. Four place names. You want the ones written with the short form Sto..Sto. TomasFalseStore 5FalseStone BridgeFalseSanto TomasFalseplaces
1 of 3 Four place names. You want the ones written with the short form Sto..
sto.ipynb

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

A store column has Lipa, lipa and Lipa . Which makes them all Lipa?
What does s.str.extract(r"(\d+) pcs") give for Eggs 30 pcs?
s.str.contains("3.5 kg") also matches 3-5 kg. Why?

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.

All sources for Simple Data Analysis