創造 WindowSpec 一個並定義了排序。
語法
Window.orderBy(*cols)
參數
| 參數 | 類型 | 說明 |
|---|---|---|
cols |
str、欄位或列表 | 欄位名稱或表達式。 |
退貨
WindowSpec
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"])
# Show row number ordered by category in each id partition.
window = Window.partitionBy("id").orderBy("category")
df.withColumn("row_number", sf.row_number().over(window)).show()
# +---+--------+----------+
# | id|category|row_number|
# +---+--------+----------+
# | 1| a| 1|
# | 1| a| 2|
# | 1| b| 3|
# | 2| a| 1|
# | 2| b| 2|
# | 3| b| 1|
# +---+--------+----------+