Window.
orderBy
Creates a WindowSpec with the ordering defined.
WindowSpec
New in version 1.4.0.
Column
names of columns or expressions
WindowSpec A WindowSpec with the ordering defined.
Examples
>>> from pyspark.sql import Window >>> from pyspark.sql.functions import row_number >>> df = spark.createDataFrame( ... [(1, "a"), (1, "a"), (2, "a"), (1, "b"), (2, "b"), (3, "b")], ["id", "category"]) >>> df.show() +---+--------+ | id|category| +---+--------+ | 1| a| | 1| a| | 2| a| | 1| b| | 2| b| | 3| b| +---+--------+
Show row number order by category in partition id.
category
id
>>> window = Window.partitionBy("id").orderBy("category") >>> df.withColumn("row_number", 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| +---+--------+----------+