Skip to main content

percent_rank

Window function: returns the relative rank (i.e. percentile) of rows within a window partition.

Syntax

Python
from pyspark.sql import functions as sf

sf.percent_rank()

Parameters

This function does not take any parameters.

Returns

pyspark.sql.Column: the column for calculating relative rank.

Examples

Python
from pyspark.sql import functions as sf
from pyspark.sql import Window
df = spark.createDataFrame([1, 1, 2, 3, 3, 4], "int")
w = Window.orderBy("value")
df.withColumn("pr", sf.percent_rank().over(w)).show()
Output
+-----+---+
|value| pr|
+-----+---+
| 1|0.0|
| 1|0.0|
| 2|0.4|
| 3|0.6|
| 3|0.6|
| 4|1.0|
+-----+---+