【问题标题】:How to remove elements from an array Column in Spark?如何从 Spark 中的数组列中删除元素?
【发布时间】:2019-10-04 11:02:12
【问题描述】:

我有一个Seq 和数据框。数据框包含一列数组类型。我正在尝试从列中删除 Seq 中的元素。

例如:

val stop_words = Seq("a", "and", "for", "in", "of", "on", "the", "with", "s", "t")

    +---------------------------------------------------+
    |sorted_items                                       |
    +---------------------------------------------------+
    |[flannel, and, for, s, shirts, sleeve, warm]       |
    |[3, 5, kitchenaid, s]                              |
    |[5, 6, case, flip, inch, iphone, on, xs]           |
    |[almonds, chocolate, covered, dark, joe, s, the]   |
    |null                                               |
    |[]                                                 |
    |[animation, book]                                  |

预期输出:

+---------------------------------------------------+
|sorted_items                                       |
+---------------------------------------------------+
|[flannel, shirts, sleeve, warm]                    |
|[3, 5, kitchenaid]                                 |
|[5, 6, case, flip, inch, iphone, xs]               |
|[almonds, chocolate, covered, dark, joe, the]      |
|null                                               |
|[]                                                 |
|[animation, book]                                  |

如何以有效和优化的方式做到这一点?

【问题讨论】:

    标签: arrays scala apache-spark dataframe seq


    【解决方案1】:

    使用来自spark.sql.functionsarray_except

    import org.apache.spark.sql.{functions => F}
    
    val stopWords = Array("a", "and", "for", "in", "of", "on", "the", "with", "s", "t")
    
    val newDF = df.withColumn("sorted_items", F.array_except(df("sorted_items"), F.lit(stopWords)))
    
    newDF.show(false)
    

    输出:

    +----------------------------------------+
    |sorted_items                            |
    +----------------------------------------+
    |[flannel, shirts, sleeve, warm]         |
    |[3, 5, kitchenaid]                      |
    |[5, 6, case, flip, inch, iphone, xs]    |
    |[almonds, chocolate, covered, dark, joe]|
    |null                                    |
    |[]                                      |
    |[animation, book]                       |
    +----------------------------------------+
    

    【讨论】:

    • 这个怎么翻译成SQL?
    【解决方案2】:

    使用 MLlib 包中的 StopWordsRemover。可以使用setStopWords 函数设置自定义停用词。 StopWordsRemover 不会处理空值,因此需要在使用前处理这些值。可以这样做:

    val df2 = df.withColumn("sorted_values", coalesce($"sorted_values", array()))
    
    val remover = new StopWordsRemover()
      .setStopWords(stop_words.toArray)
      .setInputCol("sorted_values")
      .setOutputCol("filtered")
    
    val df3 = remover.transform(df2)
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-14
    • 2022-08-11
    • 2021-10-23
    相关资源
    最近更新 更多