如何提高在数据帧中使用模糊匹配的速度?
How to increase the speed of using fuzzy matching in dataframe?
我想使用模糊匹配来检查数据框是否包含关键字。
但是用起来很慢 apply
.
有没有更快的方法?
我们可以使用 str
或 re
吗?
import regex
result = df['sentence'].apply(lambda x: regex.compile('(keyword){e<4}').findall(x)) #slow
非常感谢。
为什么要在应用程序中编译?这实际上违背了它的目的。此外,加快 apply
调用的最佳方法是不使用 apply
.
在没有你实际想要匹配的上下文的情况下,我向你展示:
p = regex.compile('(keyword){e<4}')
result = [p.findall(x) for x in df['sentence']]
好吧,对此持保留态度,因为它始终取决于您的数据和您要匹配的内容。
如果您只想进行一次匹配(以获得更高性能),您可能需要考虑使用 re.search
而不是 findall。
我想使用模糊匹配来检查数据框是否包含关键字。
但是用起来很慢 apply
.
有没有更快的方法?
我们可以使用 str
或 re
吗?
import regex
result = df['sentence'].apply(lambda x: regex.compile('(keyword){e<4}').findall(x)) #slow
非常感谢。
为什么要在应用程序中编译?这实际上违背了它的目的。此外,加快 apply
调用的最佳方法是不使用 apply
.
在没有你实际想要匹配的上下文的情况下,我向你展示:
p = regex.compile('(keyword){e<4}')
result = [p.findall(x) for x in df['sentence']]
如果您只想进行一次匹配(以获得更高性能),您可能需要考虑使用 re.search
而不是 findall。