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
| 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