【问题标题】:How to drop rows that contain certain value, while maintaining rows with empty values?如何删除包含特定值的行,同时保留具有空值的行?
【发布时间】:2019-07-15 00:32:48
【问题描述】:

我在 Spark Scala 中有以下小型演示 DataFrame:

Type   Description
0      
1      Action 1
1      Drop: Action 1
2      Action2

我需要删除Description 列中包含“Drop”的所有行,同时保留Description 为空的行。

预期结果:

Type   Description
0      
1      Action 1
2      Action2

如果我运行下面显示的代码,我会得到这个输出(Description 为空的行被删除)。

Type   Description
1      Action 1
2      Action2

我的代码:

df
  .na.fill("Description", Seq("Error"))
  .filter(!(col("Description").contains("Drop")))
  .select(col("Type"),col("Description"))
  .distinct
  .sort(col("Type").asc)
  .show()

【问题讨论】:

    标签: scala apache-spark apache-spark-sql


    【解决方案1】:

    您为na.fill 提供的参数不正确。如果您查看正在使用的方法的签名:

    def fill(value: String, cols: Seq[String]): DataFrame
    

    “第一个参数”是替换null 列的value,“第二个参数”是替换null-替换的list of columns。因此,用以下代码替换您的 na.fill 代码行将按预期工作:

    na.fill("Error", Array("Description"))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-05
      • 1970-01-01
      • 2018-11-02
      • 2014-10-04
      • 2014-06-23
      • 2021-12-14
      • 2013-07-31
      相关资源
      最近更新 更多