删除Pyspark数据帧中包含少于n个单词的行



我有一个**Pyspark数据框架**由大约600万行组成。数据集的结构如下:
root
|-- content: string (nullable = true)
|-- score: string (nullable = true)
+--------------------+-----+
|               text |score|
+--------------------+-----+
|word word hello d...|    5|
|hi man how are yo...|    5|
|come on guys let ...|    5|
|do you like some ...|    1|
|accept              |    1|
+--------------------+-----+

是否有一种方法可以删除所有只包含至少4个单词的句子的行?为了删除所有的行与几个字。
我这样做了,但是花了很长时间:

pandasDF = df.toPandas()
cnt = 0
ind = []
for index, row in pandasDF.iterrows():
txt = row["text"]
spl = txt.split()
if((len(spl)) < 4):
ind.append(index)
cnt += 1
pandasDF = pandasDF.drop(labels=ind, axis=0) 

是否有办法做这个更快,把我的Pyspark数据框架变成一个Pandas数据框架?

每个text可以用split拆分为单个单词,然后可以用size计算单词的数量:

from pyspark.sql import functions as F
df.filter( F.size(F.split('text', ' ')) >= 4).show()

这个语句只保留包含至少4个单词的行。

最新更新