Python Pandas Series.str.match()

Python Pandas Series.str.match()

Series.str可以用来将系列的值作为字符串访问,并对其应用几种方法。Pandas Series.str.match()函数用于确定给定系列对象的底层数据中的每个字符串是否与正则表达式匹配。

语法:

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

参数:
pat : 带有捕获组的正则表达式模式。
case : 如果为真,则区分大小写。
flags :一个re模块的标志,例如re.IGNORECASE。
na : 缺省的NaN,为缺失值填充值。

返回:系列/布尔值阵列

示例#1:使用Series.str.match()函数将传递的正则表达式与给定系列对象的底层数据中的字符串进行匹配。

# 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)
Python

输出 :

Python Pandas Series.str.match()

现在我们将使用Series.str.match()函数将传递的正则表达式与给定系列对象的底层数据中的字符串进行匹配。

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

输出 :

Python Pandas Series.str.match()

正如我们在输出中看到的,Series.str.match()函数返回了一系列的布尔值。对于那些成功匹配的值,它包含True,否则它包含False。

示例#2 :使用Series.str.match()函数将传递的正则表达式与给定系列对象的基础数据中的字符串进行匹配。

# 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)
Python

输出 :

Python Pandas Series.str.match()

现在我们将使用Series.str.match()函数将传递的正则表达式与给定系列对象的底层数据中的字符串进行匹配。

# 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)
Python

输出 :

Python Pandas Series.str.match()

正如我们在输出中看到的,Series.str.match()函数返回了一系列的布尔值。对于那些成功匹配的值,它包含True,否则它包含False。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册