search
Search
Login
Unlock 100+ guides
menu
menu
web
search toc
close
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
What does this mean?
Why is this true?
Give me some examples!
search
keyboard_voice
close
Searching Tips
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
icon_star
Doc Search
icon_star
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Navigate to

Pandas Series str | extract method

schedule Aug 12, 2023
Last updated
local_offer
PythonPandas
Tags
mode_heat
Master the mathematics behind data science with 100+ top-tier guides
Start your free 7-days trial now!

Pandas Series str.extract(~) extracts the first matched substrings using regular expression.

NOTE

To extract all matches instead of just the first one, use str.extractall(~).

Parameters

1. patlink | str

Regular expression to match.

2. flags | int | optional

The flags to set from the re library (e.g. re.IGNORECASE). Multiple flags can be set by combining them with the bitwise | (e.g. re.IGNORECASE | re.MULTILINE).

3. expandlink | boolean | optional

  • If True, then a pattern with one group will return DataFrame.

  • If False, then a pattern with one group will return Series or Index.

By default, expand=True.

Return Value

  • If expand=True, then a DataFrame is returned.

  • If expand=False, then a pattern with one group will return Series or Index.

  • In case of multiple capturing groups, then a DataFrame is returned regardless of expand.

Examples

Basic usage

Consider the following DataFrame:

import pandas as pd
df = pd.DataFrame({'A':['a1','b2','c3']})
df
A
0 a1
1 b2
2 c3

To get extract substrings that match a given regex:

df['A'].str.extract('[ab](\d+)')
0
0 1
1 2
2 NaN

Here, [ab] means either a or b, and \d+ denotes a number. We use () to indicate the part we want to extract.

Multiple capturing groups

We can capture multiple groups using multiple brackets like so:

df['A'].str.extract('([ab])(\d+)') # returns a DataFrame
0 1
0 a 1
1 b 2
2 NaN NaN

Setting expand

Consider the following DataFrame:

import pandas as pd
df = pd.DataFrame({'A':['a1','b2','c3']})
df
A
0 a1
1 b2
2 c3

By default, expand=True, which means that even if there is only one capturing group, a DataFrame will be returned:

df['A'].str.extract('[ab](\d+)') # expand=True
0
0 1
1 2
2 NaN

To get a Series (or Index) instead, set expand=False:

df['A'].str.extract('[ab](\d+)', expand=False)
0 1
1 2
2 NaN
Name: A, dtype: object
robocat
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
5
thumb_down
2
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!