🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpandas

pandas Documentation

LOADING ENGINE...

Series.str.extract()

AI & DATA SCIENCE // series-str-extract

series.str.extract() pulls out substrings matching regex capture groups from each element of a Series, returning them as separate columns of a new DataFrame.

Syntax

series.str.extract(pat, expand=True)

Deep Dive Course

extract() requires the regex pattern to contain at least one capture group, parentheses in the pattern, and it returns exactly one column per capture group, with the matched text from each group becoming that column's value — elements that don't match the pattern at all produce NaN in every resulting column. This makes it the standard tool for pulling structured pieces of information, like an area code, or a specific ID format, out of otherwise unstructured or semi-structured text.

1Understanding Series.str.extract()

extract() requires the regex pattern to contain at least one capture group, parentheses in the pattern, and it returns exactly one column per capture group, with the matched text from each group becoming that column's value — elements that don't match the pattern at all produce NaN in every resulting column. This makes it the standard tool for pulling structured pieces of information, like an area code, or a specific ID format, out of otherwise unstructured or semi-structured text.

💡

extract() requires at least one capture group, parentheses, in the regex pattern — a pattern with no capture groups raises an error, since extract() has no group's matched text to actually return as a column.

editor.html
import pandas as pd

s = pd.Series(["Order-1023", "Order-2045"])
print(s.str.extract(r"Order-(\d+)"))
localhost:3000

2Practical Example

Here is a real-world application of Series.str.extract() showing how it is used in production Pandas code.

editor.html
import pandas as pd

s = pd.Series(["Alice:30", "Bob:25"])
print(s.str.extract(r"(?P<name>\w+):(?P<age>\d+)"))
localhost:3000

3Best Practices

Follow these guidelines when working with Series.str.extract():

1. Use named capture groups in the regex pattern to get meaningfully-named columns directly from extract(), instead of generic numbered columns

2. Use extractall() instead of extract() when a string might contain multiple matches that all need to be captured, not just the first one

3. Test the regex pattern against representative sample values first, since a pattern that doesn't match produces silent NaN rather than an obvious error

⚠️

Tip: extract() requires at least one capture group, parentheses, in the regex pattern — a pattern with no capture groups raises an error, since extract() has no group's matched text to actually return as a column.

editor.html
import pandas as pd

s = pd.Series(["Order-1023", "Order-2045"])
print(s.str.extract(r"Order-(\d+)"))
localhost:3000

Examples

Example 01Basic Usage
import pandas as pd

s = pd.Series(["Order-1023", "Order-2045"])
print(s.str.extract(r"Order-(\d+)"))
Example 02Advanced Example
import pandas as pd

s = pd.Series(["Alice:30", "Bob:25"])
print(s.str.extract(r"(?P<name>\w+):(?P<age>\d+)"))

Best Practices

  • Use named capture groups in the regex pattern to get meaningfully-named columns directly from extract(), instead of generic numbered columns
  • Use extractall() instead of extract() when a string might contain multiple matches that all need to be captured, not just the first one
  • Test the regex pattern against representative sample values first, since a pattern that doesn't match produces silent NaN rather than an obvious error

Interview Question

What happens to an element that doesn't match the pattern at all when using series.str.extract()?

Hint: Think about what value there is to put in the resulting columns for a non-matching element.

For any element that doesn't match the pattern at all, extract() has no matched text for any of the capture groups to actually return, so it fills every resulting column with NaN for that row, rather than raising an error or leaving that row out entirely. This means the result always has the exact same number of rows as the original Series, with non-matching elements simply represented as a row of missing values across all the extracted columns.

Exercises

MediumPractice using Series.str.extract() in a real scenario.
View Solution
import pandas as pd

s = pd.Series(["Order-1023", "Order-2045"])
print(s.str.extract(r"Order-(\d+)"))

Frequently Asked Questions

What happens to an element that doesn't match the pattern at all when using series.str.extract()?

For any element that doesn't match the pattern at all, extract() has no matched text for any of the capture groups to actually return, so it fills every resulting column with NaN for that row, rather than raising an error or leaving that row out entirely. This means the result always has the exact same number of rows as the original Series, with non-matching elements simply represented as a row of missing values across all the extracted columns.

Related Functions

series-str-containsseries-str-splitre-module