
- 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
TheSeries.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 PandasSeries.str.extractall() method −
Series.str.extractall(pat, flags=0)
Parameters
TheSeries.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 asre.IGNORECASE. Multiple flags can be combined using the bitwise OR operator.
Return Value
TheSeries.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 stringss = pd.Series(['abc123def', '456ghi789', '000jkl'])# Extract all digit groups from the stringsresult = 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 abc123def1 456ghi7892 000jkldtype: objectExtracted Groups: 0 match 0 0 1231 0 456 1 7892 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 stringss = pd.Series(['name: John, age: 30', 'name: Larry, age: 25', 'name: Mark, age: 35'])# Extract name and age using named capture groupspattern = 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: 301 name: Larry, age: 252 name: Mark, age: 35dtype: objectExtracted Groups with Named Capture Groups: name age match 0 0 John 301 0 Larry 252 0 Mark 35
Example 3
This example demonstrates using there.IGNORECASE flag to extract matches in a case-insensitive manner.
import pandas as pdimport re# Create a Series of stringss = pd.Series(['Python', 'python', 'PYTHON', 'Pandas', 'pandas', 'PANDAS'])# Extract all occurrences of 'python' or 'pandas' in a case-insensitive mannerpattern = 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 Python1 python2 PYTHON3 Pandas4 pandas5 PANDASdtype: objectExtracted Groups with Case-Insensitive Matching: 0 match 0 0 Python1 0 python2 0 PYTHON3 0 Pandas4 0 pandas5 0 PANDAS