liveBook · Manning

Regular expression

As explained in Pandas Workout: 200 exercises to make you a stronger data analyst

A regular expression is a pattern notation for describing and matching sets of text strings.

A regular expression is a compact pattern for describing and matching text. In pandas, str.contains turns that pattern into a Boolean Series, which can then act as a mask for selecting matching values. section 9.0

Find words containing a or e

For a Series s, use s.str.contains('[ae]', regex=True) to test each word. Applying the result with s.loc[...] selects the matching values. The character class [ae] means either a or e; regex=True makes the intended interpretation explicit. section 9.0

Count capitalized words

Use s.str.contains('^[A-Z]\\w*$', regex=True) to match words beginning with a capital letter and continuing through zero or more word characters. Strip punctuation first when quotes or other punctuation may precede the initial capital, then filter and count the resulting Series. section 9.2

Regular expressions work especially well when combined with other string operations: normalize case, remove punctuation with str.strip, apply the pattern, and use the Boolean result for further analysis. section 9.2

Table 9.1 What you need to know (view table figure)

Concept

What is it?

Example

To learn more

s.explode

Returns a new series with each element on its own line

s.explode()

http://mng.bz/RxDP

str.contains

Returns a series of booleans, indicating which elements of the input series contain the target string

s.str.contains('a')

http://mng.bz/2D2X

str.get_dummies

Returns a data frame containing 1s and 0s based on a categorical series

s['country']. get_dummies (sep=';')

http://mng.bz/1q2g

str.index

Returns a series of integers, each indicating where the target string was found in the corresponding element of the input series

s.str.index('a')

http://mng.bz/PzEP

str.len

Returns a series of integers indicating the length of each element

s.str.len()

http://mng.bz/Jg0v

str.replace

Returns a series based on an existing series, replacing the first argument with the second

s.str.replace('a', 'e')

http://mng.bz/wv6Q

str.split

Returns a series in which each element is a list of strings; the argument specifies the delimiter used to perform the split

s.str.split(';')

http://mng.bz/qrD2

str.strip

Returns a series of Python strings without the argument’s characters on either side

s.str.strip('.!?')

http://mng.bz/7D2y

s.isin

Returns a boolean series indicating whether a value in s is an element of the argument

s.isin(['A', 'B', 'C'])

http://mng.bz/mVW2

i.intersection

Returns a new index object containing elements in two existing index objects

i.intersection(i2)

http://mng.bz/5w21

Use the string-method reference to compare str.contains with related Series operations such as str.strip, str.len, str.index, and str.replace. section 9.0

How Pandas Workout: 200 exercises to make you a stronger data analyst covers it

How deep it goes
Explains character classes, anchors, word characters, Boolean masks, and preprocessing, but stops short of a general regex syntax survey.
Examples
Demonstrates matching words containing vowels and counting capitalized words after removing surrounding punctuation.
What it assumes
Assumes familiarity with pandas Series and string accessors while teaching pattern matching through concrete character classes and anchors.
What you can do after
Apply it via pandas str.contains to build text filters and count values matching explicit patterns.
How it connects
Connects regular expressions with pandas Series, string methods, Boolean indexing, punctuation stripping, and value counting.

Also explained in

Compare how 27 books explain regular expression →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pandas Workout: 200 exercises to make you a stronger data analyst. The section links point to the parts of the book it draws on.