根据列的最大值过滤火花数据帧

问题描述 投票:0回答:3

我想做这样的事情:

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

你能告诉我一种实现我想要的方法吗?

scala apache-spark spark-dataframe
3个回答
3
投票

我认为newCol已经存在于df,然后:

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())你也可以jjst写max($"newCol").over()


0
投票

我认为数据帧describe function正是你要找的。

ds.describe("age", "height").show()

// output:  
// summary age   height  
// count   10.0  10.0  
// mean    53.3  178.05  
// stddev  11.6  15.7  
// min     18.0  163.0  
// max     92.0  192.0  

0
投票

我将两个步骤分开:

val newDF = df
 .withColumn("newCol", <some formula>)

// Spark 2.1 or later
// With 1.x use join
newDf.alias("l").crossJoin(
  newDf.alias("r")).where($"l.newCol" > least($"r.newCol", lit(10.0)))

要么

newDf.where(
  $"newCol" > (newDf.select(max($"newCol")).as[Double].first min 10.0))
© www.soinside.com 2019 - 2024. All rights reserved.