如何从特定列中具有最大值的数据框中获取行? [英] How to get the row from a dataframe that has the maximum value in a specific column?
本文介绍了如何从特定列中具有最大值的数据框中获取行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个这样的数据框
df.show(5)
kv |list1 |list2 |p
[k1,v2|[1,2,5,9 |[5,1,7,9,6,3,1,4,9] |0.5
[k1,v3|[1,2,5,8,9|[5,1,7,9,6,3,1,4,15] |0.9
[k2,v2|[77,2,5,9]|[0,1,8,9,7,3,1,4,100]|0.01
[k5,v5|[1,0,5,9 |[5,1,7,9,6,3,1,4,3] |0.3
[k9,v2|[1,2,5,9 |[5,1,7,9,6,3,1,4,200]|2.5
df.count()
5200158
我想获得最大p的行,下面的这个对我有用,但是我不知道是否还有另一种更干净的方法
I want to get the row that have maximum p, this below works for me but I don't know if there is another cleaner way
val f = df.select(max(struct(
col("pp") +: df.columns.collect { case x if x != "p" => col(x) }: _*
))).first()
推荐答案
只需排序,然后取:
import org.apache.spark.sql.functions.desc
df.orderBy(desc("pp")).take(1)
或
df.orderBy(desc("pp")).limit(1).first
这篇关于如何从特定列中具有最大值的数据框中获取行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文