如何使用Java中的spark在Dataframe中使用特定值替换空值? [英] How to replace null values with a specific value in Dataframe using spark in Java?

查看:291
本文介绍了如何使用Java中的spark在Dataframe中使用特定值替换空值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试提高使用Java在Spark中实现的Logistic回归算法的准确性。为此,我试图用该列的最频繁值替换列中存在的Null或无效值。例如: -

I am trying improve the accuracy of Logistic regression algorithm implemented in Spark using Java. For this I'm trying to replace Null or invalid values present in a column with the most frequent value of that column. For Example:-

Name|Place
a   |a1
a   |a2
a   |a2
    |d1
b   |a2
c   |a2
c   |
    |
d   |c1

在这种情况下,我将替换Name列中的所有NULL值与'a'和列'Place'与'a2'。到目前为止,我只能提取特定列中最常用的列。你能否帮我解决如何用该列中最常见的值替换空值或无效值的第二步。

In this case I'll replace all the NULL values in column "Name" with 'a' and in column "Place" with 'a2'. Till now I am able to extract only the most frequent columns in a particular column. Can you please help me with the second step on how to replace the null or invalid values with the most frequent values of that column.

推荐答案

您可以使用 .na.fill 函数(它是 org.apache.spark.sql.DataFrameNaFunctions )。

You can use .na.fill function (it is a function in org.apache.spark.sql.DataFrameNaFunctions).

基本上你需要的函数是: def fill(value:String,cols:Seq [String]):DataFrame

Basically the function you need is: def fill(value: String, cols: Seq[String]): DataFrame

您可以选择列,然后选择要替换空值或NaN的值。

You can choose the columns, and you choose the value you want to replace the null or NaN.

在您的情况下,它将类似于:

In your case it will be something like:

val df2 = df.na.fill("a", Seq("Name"))
            .na.fill("a2", Seq("Place"))

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆