Movatterモバイル変換


[0]ホーム

URL:


Python Pandas Tutorial

Pandas Series.str.fullmatch() Method



TheSeries.str.fullmatch() method in Pandas is used to determine if each string in the Series entirely matches a specified regular expression pattern or not.

This method is useful when you want to verify if entire strings conform to a given format or pattern. And equivalent to applyingre.fullmatch() to each string in the Series.

Syntax

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

Series.str.fullmatch(pat, case=True, flags=0, na=None)

Parameters

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

  • pat − A string representing the character sequence or regular expression pattern to match against.

  • case − A boolean value, default is True. If True, the match is case sensitive.

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

  • na − An optional scalar value used for missing values. If not specified, the default depends on the dtype of the Series. For object-dtype, numpy.nan is used. For StringDtype, pandas.NA is used.

Return Value

TheSeries.str.fullmatch() method returns a Series or Index of boolean values. Each boolean value indicates whether the corresponding string in the Series entirely matches the given regular expression pattern.

Example 1

This example demonstrates checking if each string in a Series fully matches the regular expression pattern for a valid email address.

import pandas as pd# Create a Series of stringss = pd.Series(['user@example.com', 'user@domain', 'example.com', 'test@tutorialspoint.com'])# Check if each string fully matches the pattern for an email addressresult = s.str.fullmatch(r'\w+@\w+\.\w+')print("Input Series:")print(s)print("\nFull Match Results:")print(result)

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

Input Series:0           user@example.com1                user@domain2                example.com3    test@tutorialspoint.comdtype: objectFull Match Results:0     True1    False2    False3     Truedtype: bool

The output shows that only the strings that fully match the email pattern are marked asTrue.

Example 2

This example demonstrates checking if each string fully matches the pattern for a date in the format 'YYYY-MM-DD' using theSeries.str.fullmatch() method.

import pandas as pd# Create a Series of stringss = pd.Series(['2024-07-29', '2024-07-29 00:00:00', '2024-07-29T00:00:00', '07-29-2024'])# Check if each string fully matches the date patternresult = s.str.fullmatch(r'\d{4}-\d{2}-\d{2}')print("Input Series:")print(s)print("\nFull Match Results:")print(result)

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

Input Series:0             2024-07-291    2024-07-29 00:00:002    2024-07-29T00:00:003             07-29-2024dtype: objectFull Match Results:0     True1    False2    False3    Falsedtype: bool

Example 3

This example demonstrates checking if each string in a DataFrame column fully matches a date pattern, while handling missing values.

import pandas as pd# Create a DataFrame with date stringsdf = pd.DataFrame({    'date': ['2024-07-29', '2024-07-29 00:00:00', '2024-07-29', None]})# Check if each string fully matches the date pattern, treating NaNs as Trueresult = df['date'].str.fullmatch(r'\d{4}-\d{2}-\d{2}', na=True)print("Input DataFrame:")print(df)print("\nFull Match Results:")print(result)

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

Input DataFrame:                  date0           2024-07-291  2024-07-29 00:00:002           2024-07-293                 NoneFull Match Results:0     True1    False2     True3     TrueName: date, dtype: bool

In this case, theNaN value is treated asTrue due to thena=True parameter, while other strings are matched according to the pattern.

python_pandas_working_with_text_data.htm
Print Page
Advertisements

[8]ページ先頭

©2009-2025 Movatter.jp