Spark dataframe na.fill 布尔列类型



我能够使用以下方法填充Numberic和String类型列:

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

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

有什么想法吗?

您可以在fill中使用Map,其中键是列名IntLongFloatDoubleStringBoolean

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为不同的列设置不同的值。

我希望答案对您有所帮助。

na.fill

布尔类型是在版本2.3.0中添加的,以前的版本不支持填充布尔类型列。请参阅此处的 API 规格。

最新更新