Skip to main content

spark_partition_id

A column for partition ID.

Syntax

Python
from pyspark.sql import functions as sf

sf.spark_partition_id()

Returns

pyspark.sql.Column: partition id the record belongs to.

Examples

Example 1: Get partition ID for each row

Python
from pyspark.sql import functions as sf
spark.range(10, numPartitions=5).select("*", sf.spark_partition_id()).show()
Output
+---+--------------------+
| id|SPARK_PARTITION_ID()|
+---+--------------------+
| 0| 0|
| 1| 0|
| 2| 1|
| 3| 1|
| 4| 2|
| 5| 2|
| 6| 3|
| 7| 3|
| 8| 4|
| 9| 4|
+---+--------------------+