Python 如何将正则表达式与pandas series.find函数一起使用
我试图在数据帧系列中找到正则表达式的位置,并将其分配给另一个系列。我可以用绳子做这个Python 如何将正则表达式与pandas series.find函数一起使用,python,python-3.x,pandas,Python,Python 3.x,Pandas,我试图在数据帧系列中找到正则表达式的位置,并将其分配给另一个系列。我可以用绳子做这个 df['text'].str.lower().str.find('hello') 此函数提供匹配的索引。像 text World Hello Hello WOrld WOW 用这个 df['match_ind'] = df['text'].str.lower().str.find('hello') 它给 text match_ind World Hello 6 Hello W
df['text'].str.lower().str.find('hello')
此函数提供匹配的索引。像
text
World Hello
Hello WOrld
WOW
用这个
df['match_ind'] = df['text'].str.lower().str.find('hello')
它给
text match_ind
World Hello 6
Hello WOrld 0
WOW -1
但是,我不想使用正则表达式来表示“hello | world”,而想使用正则表达式来表示“hello | world”。目前,它给了我-1
df['text'].str.lower().str.find('hello|world')
我在用蟒蛇3
pandas是否找到支持正则表达式的工具,或者是否有一些pandas方法可以做到这一点。我想您正在寻找:
是否要
.str.match('hello | world')
?更新了查找
函数的工作和输出
import re
df.text.apply(lambda x:[m.start() for m in re.finditer('hello|world',x,flags=re.I)])
0 [0, 6]
1 [0, 6]
2 []