Movatterモバイル変換


[0]ホーム

URL:


Python Pandas Tutorial

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
python_pandas_working_with_text_data.htm
Print Page
Advertisements

[8]ページ先頭

©2009-2025 Movatter.jp