
- Python Pandas - Home
- Python Pandas - Introduction
- Python Pandas - Environment Setup
- Python Pandas - Basics
- Python Pandas - Introduction to Data Structures
- Python Pandas - Index Objects
- Python Pandas - Panel
- Python Pandas - Basic Functionality
- Python Pandas - Indexing & Selecting Data
- Python Pandas - Series
- Python Pandas - Series
- Python Pandas - Slicing a Series Object
- Python Pandas - Attributes of a Series Object
- Python Pandas - Arithmetic Operations on Series Object
- Python Pandas - Converting Series to Other Objects
- Python Pandas - DataFrame
- Python Pandas - DataFrame
- Python Pandas - Accessing DataFrame
- Python Pandas - Slicing a DataFrame Object
- Python Pandas - Modifying DataFrame
- Python Pandas - Removing Rows from a DataFrame
- Python Pandas - Arithmetic Operations on DataFrame
- Python Pandas - IO Tools
- Python Pandas - IO Tools
- Python Pandas - Working with CSV Format
- Python Pandas - Reading & Writing JSON Files
- Python Pandas - Reading Data from an Excel File
- Python Pandas - Writing Data to Excel Files
- Python Pandas - Working with HTML Data
- Python Pandas - Clipboard
- Python Pandas - Working with HDF5 Format
- Python Pandas - Comparison with SQL
- Python Pandas - Data Handling
- Python Pandas - Sorting
- Python Pandas - Reindexing
- Python Pandas - Iteration
- Python Pandas - Concatenation
- Python Pandas - Statistical Functions
- Python Pandas - Descriptive Statistics
- Python Pandas - Working with Text Data
- Python Pandas - Function Application
- Python Pandas - Options & Customization
- Python Pandas - Window Functions
- Python Pandas - Aggregations
- Python Pandas - Merging/Joining
- Python Pandas - MultiIndex
- Python Pandas - Basics of MultiIndex
- Python Pandas - Indexing with MultiIndex
- Python Pandas - Advanced Reindexing with MultiIndex
- Python Pandas - Renaming MultiIndex Labels
- Python Pandas - Sorting a MultiIndex
- Python Pandas - Binary Operations
- Python Pandas - Binary Comparison Operations
- Python Pandas - Boolean Indexing
- Python Pandas - Boolean Masking
- Python Pandas - Data Reshaping & Pivoting
- Python Pandas - Pivoting
- Python Pandas - Stacking & Unstacking
- Python Pandas - Melting
- Python Pandas - Computing Dummy Variables
- Python Pandas - Categorical Data
- Python Pandas - Categorical Data
- Python Pandas - Ordering & Sorting Categorical Data
- Python Pandas - Comparing Categorical Data
- Python Pandas - Handling Missing Data
- Python Pandas - Missing Data
- Python Pandas - Filling Missing Data
- Python Pandas - Interpolation of Missing Values
- Python Pandas - Dropping Missing Data
- Python Pandas - Calculations with Missing Data
- Python Pandas - Handling Duplicates
- Python Pandas - Duplicated Data
- Python Pandas - Counting & Retrieving Unique Elements
- Python Pandas - Duplicated Labels
- Python Pandas - Grouping & Aggregation
- Python Pandas - GroupBy
- Python Pandas - Time-series Data
- Python Pandas - Date Functionality
- Python Pandas - Timedelta
- Python Pandas - Sparse Data Structures
- Python Pandas - Sparse Data
- Python Pandas - Visualization
- Python Pandas - Visualization
- Python Pandas - Additional Concepts
- Python Pandas - Caveats & Gotchas
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