Tutorial by Examples

# Extract strings with a specific regex df= df['col_name'].str.extract[r'[Aa-Zz]'] # Replace strings within a regex df['col_name'].str.replace('Replace this', 'With this') For information on how to match strings using regex, see Getting started with Regular Expressions.
Strings in a Series can be sliced using .str.slice() method, or more conveniently, using brackets (.str[]). In [1]: ser = pd.Series(['Lorem ipsum', 'dolor sit amet', 'consectetur adipiscing elit']) In [2]: ser Out[2]: 0 Lorem ipsum 1 dolor sit amet 2 cons...
str.contains() method can be used to check if a pattern occurs in each string of a Series. str.startswith() and str.endswith() methods can also be used as more specialized versions. In [1]: animals = pd.Series(['cat', 'dog', 'bear', 'cow', 'bird', 'owl', 'rabbit', 'snake']) Check if strings con...
In [1]: ser = pd.Series(['lORem ipSuM', 'Dolor sit amet', 'Consectetur Adipiscing Elit']) Convert all to uppercase: In [2]: ser.str.upper() Out[2]: 0 LOREM IPSUM 1 DOLOR SIT AMET 2 CONSECTETUR ADIPISCING ELIT dtype: object All lowercase: In [3]: ser...

Page 1 of 1