建立 WindowSpec 一個定義框架邊界的 ,從 start (包含)到 end (包含)。
和 start 都是end從當前列相對位置。 例如,表示 0 「當前列」, -1 指當前列之前的列,指 5 當前列之後的第五列。
基於列的邊界是根據該列在分割中的位置來決定的。 偏移量表示畫面起點或結束點上方或下方的列數。
語法
Window.rowsBetween(start, end)
參數
| 參數 | 類型 | 說明 |
|---|---|---|
start |
int | 邊界起始,包含。 若該框架為 Window.unboundedPreceding,或任何小於或等於 -9223372036854775808的值,則該框架為無界。 |
end |
int | 邊界端,包含。 若該框架為 Window.unboundedFollowing,或任何大於或等於 9223372036854775807的值,則該框架為無界。 |
退貨
WindowSpec
Notes
使用 Window.unboundedPreceding、 Window.unboundedFollowing、 來 Window.currentRow 指定特殊的邊界值,而非直接使用整數值。
Examples
from pyspark.sql import Window, functions as sf
df = spark.createDataFrame(
[(1, "a"), (1, "a"), (2, "a"), (1, "b"), (2, "b"), (3, "b")], ["id", "category"])
# Calculate the sum of id from the current row to current row + 1 in each category partition.
window = Window.partitionBy("category").orderBy("id").rowsBetween(Window.currentRow, 1)
df.withColumn("sum", sf.sum("id").over(window)).sort("id", "category", "sum").show()
# +---+--------+---+
# | id|category|sum|
# +---+--------+---+
# | 1| a| 2|
# | 1| a| 3|
# | 1| b| 3|
# | 2| a| 2|
# | 2| b| 5|
# | 3| b| 3|
# +---+--------+---+