from pyspark.sql.types import *
schema = StructType([StructField("type", StringType(), True), StructField("average", IntegerType(), True)])
values = [('A', 19), ('B', 17), ('C', 10)]
df = spark.createDataFrame(values, schema)
parts = df.rdd.getNumPartitions()
print(parts)
Output is 44
How is spark creating 44 partitions for 3 records dataframe?
import pyspark.sql.functions as F
df.withColumn('p_id', F.spark_partition_id()).show()
Output :
+----+-------+----+
|type|average|p_id|
+----+-------+----+
| A| 19| 14|
| B| 17| 29|
| C| 10| 43|
+----+-------+----+