rowsBetween(Window)

WindowSpec(포함)에서 start (포함)로 정의된 프레임 경계를 end 사용하여 만듭니다.

start 둘 다 end 현재 행의 상대 위치입니다. 예를 들어 0 "현재 행" -1 은 현재 행 앞의 행을 의미하고 5 현재 행 뒤의 다섯 번째 행을 의미합니다.

행 기반 경계는 파티션 내의 행 위치를 기반으로 합니다. 오프셋은 프레임이 시작되거나 끝나는 현재 행 위 또는 아래의 행 수를 나타냅니다.

문법

Window.rowsBetween(start, end)

매개 변수

매개 변수 유형 설명
start int 경계 시작(포함)입니다. 프레임이 바인딩되지 않은 경우 또는 값이 Window.unboundedPreceding1보다 작거나 같은 경우 -9223372036854775808
end int 경계 끝(포함)입니다. 프레임이 바인딩되지 않은 경우 또는 값이 Window.unboundedFollowing1보다 크거나 같은 경우 9223372036854775807

Returns

WindowSpec

Notes

를 Window.unboundedPreceding사용하고 Window.unboundedFollowing정수 Window.currentRow 값을 직접 사용하는 대신 특수 경계 값을 지정합니다.

예제

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|
# +---+--------+---+