计算行均值,而忽略Spark Scala中的NA [英] Calculate row mean, ignoring NAs in Spark Scala

查看:352
本文介绍了计算行均值,而忽略Spark Scala中的NA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找到一种方法来计算Scala的Spark数据框中的的平均值,而我想忽略NA。在R中,有一个非常方便的函数称为rowMeans,可以在其中指定忽略NA:

I'm trying to find a way to calculate the mean of rows in a Spark Dataframe in Scala where I want to ignore NAs. In R, there is a very convenient function called rowMeans where one can specify to ignore NAs:

rowmeans(df,na.rm=TRUE)

我无法找到Spark Dataframes的对应函数,我想知道是否有人有建议或意见(如果可能)。用0代替它们是不必要的,因为这会影响分母。

I'm unable to find a corresponding function for Spark Dataframes, and I wonder if anyone has a suggestion or input if this would be possible. Replacing them with 0 won't due since this will affect the denominator.

我发现了一个类似的问题此处,但是我的数据框将包含数百列。

I found a similar question here, however my dataframe will have hundreds of columns.

感谢任何帮助和共享见解,欢呼!

Any help and shared insights is appreciated, cheers!

推荐答案

通常,此类函数默认情况下会忽略空值。
即使存在一些包含数字和字符串类型的混合列,该列也会删除字符串和空值,并且仅计算数字。

Usually such functions ignore nulls by default. Even if there are some mixed columns with numeric and string types, this one will drop strings and nulls, and calculate only numerics.

df.select(df.columns.map(c => mean(col(c))) :_*).show

这篇关于计算行均值,而忽略Spark Scala中的NA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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