栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何有效检查Spark Dataframe中是否包含单词列表?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何有效检查Spark Dataframe中是否包含单词列表?

您应该考虑使用pyspark sql模块函数而不是编写a

UDF
,有几个
regexp
基于函数:

首先让我们从一个更完整的示例数据框架开始:

df = sc.parallelize([["a","b","foo is tasty"],["12","34","blah blahhh"],["yeh","0","bar of yums"],['haha', '1', 'foobar none'], ['hehe', '2', 'something bar else']])    .toDF(["col1","col2","col_with_text"])

如果要根据行是否包含中的单词之一来过滤行

words_list
,可以使用
rlike

import pyspark.sql.functions as psfwords_list = ['foo','bar']df.filter(psf.col('col_with_text').rlike('(^|s)(' + '|'.join(words_list) + ')(s|$)')).show()    +----+----+------------------+    |col1|col2|     col_with_text|    +----+----+------------------+    |   a|   b|      foo is tasty|    | yeh|   0|       bar of yums|    |hehe|   2|something bar else|    +----+----+------------------+

如果要提取与正则表达式匹配的字符串,可以使用

regexp_extract

df.withColumn(        'extracted_word',         psf.regexp_extract('col_with_text', '(?=^|s)(' + '|'.join(words_list) + ')(?=s|$)', 0))    .show()    +----+----+------------------+--------------+    |col1|col2|     col_with_text|extracted_word|    +----+----+------------------+--------------+    |   a|   b|      foo is tasty|foo|    |  12|  34|       blah blahhh|   |    | yeh|   0|       bar of yums|bar|    |haha|   1|       foobar none|   |    |hehe|   2|something bar else|   |    +----+----+------------------+--------------+


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/625365.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号