如何在Pandas身上使用regex系列.查找功能

2024-05-16 10:24:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在一个数据帧序列中找到正则表达式的位置,并将其分配给另一个序列。我可以用绳子做这个

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,比如说'hello|world'。现在,它给了我-1

df['text'].str.lower().str.find('hello|world')

我在用Python3

熊猫找到支持正则表达式还是有一些熊猫的方法来做到这一点。你知道吗


Tags: 数据texthellodfworldmatch序列find