![](/img/trans.png)
[英]how to access the column index for spark dataframe in scala for calculation
[英]How to calculate the average of rows between a start index and end index of a column in a dataframe in Spark using Scala?
我有一個火花數據框,其中的列具有浮點類型值。 我試圖找到第11行到第20行之間的平均值。請注意,我沒有嘗試任何移動平均值。 我試過像這樣使用分區窗口-
var avgClose= avg(priceDF("Close")).over(partitionWindow.rowsBetween(11,20))
它返回一個“ org.apache.spark.sql.Column”結果。 我不知道如何查看avgClose
。
我是Spark和Scala的新手。 感謝您的幫助。
為您的表分配一個遞增的ID。 然后,您可以對ID進行平均。
val df = Seq(20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1).toDF("val1")
val dfWithId = df.withColumn("id", monotonically_increasing_id())
val avgClose= dfWithId.filter($"id" >= 11 && $"id" <= 20).agg(avg("val1"))
avgClose.show()
結果:
+---------+
|avg(val1)|
+---------+
| 5.0|
+---------+
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.