Open In App

Python | Pandas Series.str.match()

Improve
Improve
Like Article
Like
Save
Share
Report

Series.str can be used to access the values of the series as strings and apply several methods to it. Pandas Series.str.match() function is used to determine if each string in the underlying data of the given series object matches a regular expression.

Python Pandas Series.str.match()

Syntax: Series.str.match(pat, case=True, flags=0, na=nan)

Parameter :

  • pat: Regular expression pattern with capturing groups.
  • case: If True, case sensitive
  • flags: A re-module flag, for example, re.IGNORECASE.
  • na: default, NaN, fill value for missing values

Returns: Series/array of boolean values

Series.str.match() in Pandas Examples

Below are the examples by which can perform string matching in Pandas Series in Python:

  • Match Patterns in Pandas Series Using Series.str.match()
  • Match Groups Having Capital Letters
  • Matching Multiple Conditions
  • Matching with Flags
  • Using Named Groups to Match Patterns in Pandas

Example 1: Match Patterns in Pandas Series Using Series.str.match()

Use Series.str.match() function to match the passed regular expressions with the string in the underlying data of the given series object.

Python3




# importing pandas as pd
import pandas as pd
 
# importing re for regular expressions
import re
 
# Creating the Series
sr = pd.Series(['New_York', 'Lisbon', 'Tokyo', 'Paris', 'Munich'])
 
# Creating the index
idx = ['City 1', 'City 2', 'City 3', 'City 4', 'City 5']
 
# set the index
sr.index = idx
 
# Print the series
print(sr)


Output:

City 1    New_York
City 2 Lisbon
City 3 Tokyo
City 4 Paris
City 5 Munich
dtype: object

Now we will use Series.str.match() function to match the passed regular expressions with the string in the underlying data of the given series object.

Python3




# match either 'Tokyo' or 'Paris'
result = sr.str.match(pat='(Tokyo)|(Paris)')
 
# print the result
print(result)


Output:

City 1    False
City 2 False
City 3 True
City 4 True
City 5 False
dtype: bool

As we can see in the output, the Series.str.match() function has returned a series of boolean values. It contains True for those values which successfully matches else it contains False.

Example 2: Match Groups Having Capital Letters

Use Series.str.match() function to match the passed regular expressions with the string in the underlying data of the given series object.

Python3




# importing pandas as pd
import pandas as pd
 
# importing re for regular expressions
import re
 
# Creating the Series
sr = pd.Series(['Mike', 'Alessa', 'Nick', 'Kim', 'Britney'])
 
# Creating the index
idx = ['Name 1', 'Name 2', 'Name 3', 'Name 4', 'Name 5']
 
# set the index
sr.index = idx
 
# Print the series
print(sr)


Output:

Name 1       Mike
Name 2 Alessa
Name 3 Nick
Name 4 Kim
Name 5 Britney
dtype: object

Now we will use Series.str.match() function to match the passed regular expressions with the string in the underlying data of the given series object.

Python3




# match groups having any capital letter
# followed by 'i' and any other character
result = sr.str.match(pat='([A-Z]i.)')
 
# print the result
print(result)


Output:

 Name 1    False
Name 2 True
Name 3 True
Name 4 False
Name 5 True
dtype: bool

As we can see in the output, the Series.str.match() function has returned a series of boolean values. It contains True for those values which successfully matches else it contains False.

Example 3: Matching Multiple Conditions

In this example, a Pandas Series s with fruit names is created. The str.match() method is then used to check if each name starts with either ‘A’ or ‘C’, producing a Boolean Series as output.

Python3




import pandas as pd
 
# Create a Series
s = pd.Series(['Apple', 'Banana', 'Cherry', 'Avocado'])
 
result = s.str.match('^(A|C)')
print(result)


Output:

0     True
1 False
2 True
3 True
dtype: bool

Example 4: Matching with Flags

In this example, a Pandas Series s containing various case variants of ‘Apple’ is created. The str.match() method with the IGNORECASE flag is used to match strings starting with ‘a’ regardless of case, resulting in a Boolean Series.

Python3




import pandas as pd
 
# Create a Series
s = pd.Series(['Apple', 'apple', 'APPLE', 'aPPle'])
 
result = s.str.match('^a', flags=re.IGNORECASE)
print(result)


Output:

0     True
1 True
2 True
3 False
dtype: bool

Example 5: Using Named Groups to Match Patterns in Pandas

In this example, a Pandas Series s with full names is created. The str.match() method is utilized with named groups to extract and match first and last names from each string, producing a Boolean Series as output.

Python3




import pandas as pd
 
# Create a Series
s = pd.Series(['John Doe', 'Jane Smith', 'Alice'])
 
result = s.str.match('^(?P<First>[A-Za-z]+) (?P<Last>[A-Za-z]+)$')
print(result)


Output:

0     True
1 True
2 False
dtype: bool



Last Updated : 22 Dec, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads