Pandas Series.str.extractall() Method



The Series.str.extractall() method in Pandas is used to extract capture groups from all matches of a regular expression pattern in a Series. The extracted groups are returned as columns in a DataFrame.

This method is particularly useful for extracting multiple matches of patterns within each string element of a Series. and can be useful for text data analysis and text-processing application, especially when dealing with strings containing multiple patterns that need to be extracted.

Syntax

Following is the syntax of the Pandas Series.str.extractall() method −

Series.str.extractall(pat, flags=0)

Parameters

The Series.str.extractall() method accepts the following parameters −

  • pat − A string representing the regular expression pattern with capturing groups.

  • flags − An optional integer, default is 0 (no flags). Flags from the re module can be used, such as re.IGNORECASE. Multiple flags can be combined using the bitwise OR operator.

Return Value

The Series.str.extractall() method returns a DataFrame with one row for each match and one column for each group. The rows have a MultiIndex, with the first levels coming from the subject Series and the last level named 'match' to index the matches in each item of the Series. Capture group names from the regular expression pattern will be used for column names; otherwise, capture group numbers will be used.

Example 1

This example demonstrates extracting all matches of a pattern from each string element in a Series.

import pandas as pd

# Create a Series of strings
s = pd.Series(['abc123def', '456ghi789', '000jkl'])

# Extract all digit groups from the strings
result = s.str.extractall(r'(\d+)')

print("Input Series:")
print(s)
print("\nExtracted Groups:")
print(result)

When we run the above code, it produces the following output −

Input Series:
0    abc123def
1    456ghi789
2       000jkl
dtype: object

Extracted Groups:
         0
  match    
0 0    123
1 0    456
  1    789
2 0    000

Example 2

This example demonstrates extracting named capture groups from each string element in a Series.

import pandas as pd

# Create a Series of strings
s = pd.Series(['name: John, age: 30', 'name: Larry, age: 25', 'name: Mark, age: 35'])

# Extract name and age using named capture groups
pattern = r'name: (?P<name>\w+), age: (?P<age>\d+)'
result = s.str.extractall(pattern)

print("Input Series:")
print(s)
print("\nExtracted Groups with Named Capture Groups:")
print(result)

Following is the output of the above code −

Input Series:
0     name: John, age: 30
1    name: Larry, age: 25
2     name: Mark, age: 35
dtype: object

Extracted Groups with Named Capture Groups:
          name age
  match           
0 0       John  30
1 0      Larry  25
2 0       Mark  35

Example 3

This example demonstrates using the re.IGNORECASE flag to extract matches in a case-insensitive manner.

import pandas as pd
import re

# Create a Series of strings
s = pd.Series(['Python', 'python', 'PYTHON', 'Pandas', 'pandas', 'PANDAS'])

# Extract all occurrences of 'python' or 'pandas' in a case-insensitive manner
pattern = r'(python|pandas)'
result = s.str.extractall(pattern, flags=re.IGNORECASE)

print("Input Series:")
print(s)
print("\nExtracted Groups with Case-Insensitive Matching:")
print(result)

Following is the output of the above code −

Input Series:
0    Python
1    python
2    PYTHON
3      Pandas
4      pandas
5      PANDAS
dtype: object

Extracted Groups with Case-Insensitive Matching:
        0
  match      
0 0  Python
1 0  python
2 0  PYTHON
3 0    Pandas
4 0    pandas
5 0    PANDAS
python_pandas_working_with_text_data.htm
Advertisements