用 Spark Dataframe 中的空值替换空值

编程入门 行业动态 更新时间:2024-10-26 08:24:14
本文介绍了用 Spark Dataframe 中的空值替换空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我有一个包含 n 列的数据框,我想用空值替换所有这些列中的空字符串.

I have a data frame with n number of columns and I want to replace empty strings in all these columns with nulls.

我尝试使用

val ReadDf = rawDF.na.replace("columnA", Map( "" -> null));

val ReadDf = rawDF.withColumn("columnA", if($"columnA"=="") lit(null) else $"columnA" );

它们都不起作用.

任何线索将不胜感激.谢谢.

Any leads would be highly appreciated. Thanks.

推荐答案

您的第一种方法由于一个错误而失败,该错误阻止了 replace 能够用空值替换值,请参阅 这里.

Your first approach seams to fail due to a bug that prevents replace from being able to replace values with nulls, see here.

您的第二种方法失败了,因为您将驱动程序端 Scala 代码与执行程序端 Dataframe 指令混淆:您的 if-else 表达式将在 驱动程序 上被评估一次(而不是每条记录);你想用对 when 函数的调用来替换它;此外,要比较列的值,您需要使用 === 运算符,而不是 Scala 的 == ,它只是比较驱动程序端的 Column> 对象:

Your second approach fails because you're confusing driver-side Scala code for executor-side Dataframe instructions: your if-else expression would be evaluated once on the driver (and not per record); You'd want to replace it with a call to when function; Moreover, to compare a column's value you need to use the === operator, and not Scala's == which just compares the driver-side Column object:

import org.apache.spark.sql.functions._

rawDF.withColumn("columnA", when($"columnA" === "", lit(null)).otherwise($"columnA"))

这篇关于用 Spark Dataframe 中的空值替换空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-18 22:17:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/947875.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Spark   Dataframe

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!