根据列的最大值过滤火花数据帧 [英] filter spark dataframe based on maximum value of a column

查看:65
本文介绍了根据列的最大值过滤火花数据帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做这样的事情:

df
.withColumn("newCol", <some formula>)
.filter(s"""newCol > ${(math.min(max("newCol").asInstanceOf[Double],10))}""")

我得到的异常:

org.apache.spark.sql.Column cannot be cast to java.lang.Double

您能建议我实现我想要的方法吗?

Can you please suggest me a way to achieve what i want?

推荐答案

我假定df中已经存在newCol,然后:

I assume newCol is already present in df, then:

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

df
.withColumn("max_newCol",max($"newCol").over(Window.partitionBy()))
.filter($"newCol"> least($"max_newCol",lit(10.0)))

除了max($"newCol").over(Window.partitionBy()),您还可以先写max($"newCol").over()

这篇关于根据列的最大值过滤火花数据帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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