行間(ウィンドウ)
フレーム境界がstart (含む)からend (含む)まで定義されたWindowSpecを作成します。
startとendはどちらも現在の行からの相対位置です。例えば、 0 「現在の行」を意味し、 -1現在の行の前の行を意味し、 5現在の行の5行後を意味します。
行ベースの境界は、パーティション内における行の位置に基づいています。オフセットとは、フレームの開始または終了位置となる現在の行から、上下何行上にあるかを示す値です。
構文
Window.rowsBetween(start, end)
パラメーター
パラメーター | Type | 説明 |
|---|---|---|
| int | 境界線の開始、包括的。これが |
| int | 境界線の端点を含む。これが |
戻り値
WindowSpec
注意
整数値を直接使用する代わりに、 Window.unboundedPreceding 、 Window.unboundedFollowing 、 Window.currentRowを使用して特別な境界値を指定します。
例
Python
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|
# +---+--------+---+