【问题标题】:Spark dataframe na.fill boolean column typeSpark 数据框 na.fill 布尔列类型
【发布时间】:2018-06-28 10:48:53
【问题描述】:

我可以使用以下方法填充数字和字符串类型的列:

masterDF = masterDF.na.fill(-1)
masterDF = masterDF.na.fill("")
masterDF = masterDF.na.fill(-1.0)

但我没有找到填充布尔类型列的 api。 我试过这个:masterDF = masterDF.na.fill(false) 不支持。

有什么想法吗?

【问题讨论】:

    标签: apache-spark


    【解决方案1】:

    您可以在fill 中使用Map,其中键是列名IntLongFloat、@ 987654326@, String, Boolean.

    masterDF.na.fill(masterDF.columns.map(_ -> false).toMap)
    

    API 文档说:

    /**
    * (Scala-specific) Returns a new `DataFrame` that replaces null values.
    *
    * The key of the map is the column name, and the value of the map is the replacement value.
    * The value must be of the following type: `Int`, `Long`, `Float`, `Double`, `String`, `Boolean`.
    * Replacement values are cast to the column data type.
    *
    * For example, the following replaces null values in column "A" with string "unknown", and
    * null values in column "B" with numeric value 1.0.
    * {{{
    *   df.na.fill(Map(
    *     "A" -> "unknown",
    *     "B" -> 1.0
    *   ))
    * }}}
    *
    * @since 1.3.1
    */
    def fill(valueMap: Map[String, Any]): DataFrame = fillMap(valueMap.toSeq)
    

    您甚至可以在fill 函数中使用Map 为不同的列设置不同的值。

    希望回答对你有帮助。

    【讨论】:

      【解决方案2】:

      na.fill2.3.0版本增加了布尔类型,之前的版本不支持填充布尔类型列。请参阅 API 规范 here

      【讨论】:

        猜你喜欢
        • 2011-06-13
        • 2014-03-25
        • 1970-01-01
        • 2019-05-01
        • 1970-01-01
        • 2020-04-04
        • 1970-01-01
        • 1970-01-01
        • 2016-04-08
        相关资源
        最近更新 更多