代码之家  ›  专栏  ›  技术社区  ›  Chan

如何提高在数据帧中使用模糊匹配的速度?

  •  1
  • Chan  · 技术社区  · 6 年前

    我想使用模糊匹配来检查数据帧是否包含关键字。

    但是,使用起来很慢 apply .

    有没有更快的方法?

    我们能用吗? str re ?

    import regex
    
    result = df['sentence'].apply(lambda x: regex.compile('(keyword){e<4}').findall(x)) #slow
    

    非常感谢你。

    1 回复  |  直到 6 年前
        1
  •  2
  •   cs95 abhishek58g    6 年前

    你为什么要在申请表里面写?这真的破坏了它的目的。另外,加快 apply 电话是不用的 应用 .

    如果没有你想要匹配的内容,我向你展示:

    p = regex.compile('(keyword){e<4}')
    result = [p.findall(x) for x in df['sentence']]
    

    My tests show that a list comprehension based regex match supersedes str methods in terms of performance. 好吧,拿这个来说吧,因为它总是取决于你的数据和你想要匹配的东西。

    你可以考虑使用 re.search 而不是芬德尔,如果你只是想要一个单一的匹配(为更多的性能)。