Movatterモバイル変換


[0]ホーム

URL:


Python Pandas Tutorial

Pandas Series.str.findall() Method



TheSeries.str.findall() method in Python Pandas is used to find all occurrences of a pattern or regular expression within each string in the Series or Index. This method is equivalent to applyingre.findall() to all elements in the Series/Index.

The method returns a Series or Index of lists, where each list contains all non-overlapping matches of the pattern or regular expression found in the corresponding string. And it is useful for finding and extracting all non-overlapping occurrences of a specified pattern or regular expression from each string in a Pandas Series, Index, or a DataFrame column.

Syntax

Following is the syntax of the PandasSeries.str.findall() method −

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

Parameters

TheSeries.str.findall() method accepts the following parameters −

  • pat − A string representing the pattern or regular expression to be searched for.

  • flags − An optional integer, default is 0. Flags from the re module, such as re.IGNORECASE, to modify the pattern matching behavior.

Return Value

TheSeries.str.findall() method returns a Series or Index of lists of strings. Each list contains all non-overlapping matches of the pattern or regular expression found in the corresponding string. If no matches are found, an empty list is returned for those elements.

Example 1

This example demonstrates finding all occurrences of the substring 't' in each string element in a Series.

import pandas as pd# Create a Series of stringss = pd.Series(['tutorials', 'articles', 'Examples'])# Find all occurrences of the substring 't' in each stringresult = s.str.findall('t')print("Input Series:")print(s)print("\nOccurrences of 't':")print(result)

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

Input Series:0    tutorials1     articles2     Examplesdtype: objectOccurrences of 't':0    [t, t]1       [t]2        []dtype: object

An empty list[] indicates that there are no occurrences of the pattern in the element.

Example 2

This example demonstrates finding all occurrences of a pattern using a regular expression. Here, we look for all substrings starting with 't' followed by any character.

import pandas as pd# Create a Series of stringss = pd.Series(['tutorials', 'testing', 'test cases'])# Find all substrings starting with 't' followed by any characterresult = s.str.findall(r't.')print("Input Series:")print(s)print("\nOccurrences of pattern 't.':")print(result)

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

Input Series:0    tutorials1      testing2   test casesdtype: objectOccurrences of pattern 't.':0    [tu, to]1    [te, ti]2    [te, t ]dtype: object

The output shows lists of matches for the regular expression pattern 't.' where each element represents substrings that match the pattern.

Example 3

This example demonstrates applying theSeries.str.findall() method to a DataFrame. We find all email addresses in a DataFrame that match a specified pattern.

import pandas as pd# Create a DataFrame df = pd.DataFrame({    'Email': ['user1@example.com', 'info@tutorialspoint.com', 'contact@website.org']})# Find all occurrences of the pattern 'tutorialspoint.com' in the 'Email' columnresult = df['Email'].str.findall('tutorialspoint.com')print("Input DataFrame:")print(df)print("\nOccurrences of 'tutorialspoint.com':")print(result)

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

Input DataFrame:                      Email0          user1@example.com1  info@tutorialspoint.com2       contact@website.orgOccurrences of 'tutorialspoint.com':0    []1    [tutorialspoint.com]2    []Name: Email, dtype: object

The output shows that the pattern 'tutorialspoint.com' is found in the second email address only.

python_pandas_working_with_text_data.htm
Print Page
Advertisements

[8]ページ先頭

©2009-2025 Movatter.jp