Partition Basics

Spark tutorial · PySpark.in

How Spark Stores DataFrames Internally

Unlike Pandas, Spark does not keep a DataFrame as one giant block in memory. Instead it breaks the data into smaller chunks called partitions. These partitions are spread across the cluster so multiple tasks can process them in parallel. According to the RDD programming guide, an RDD (Resilient Distributed Dataset) is a collection of elements partitioned across the nodes of the cluster, enabling parallel operations. Spark DataFrames sit on top of RDDs, so they inherit this partitioned structure.

What is Partitions in Spark?

A partition is a subset of your DataFrame’s rows stored on one node of the cluster. You can think of a partition like a chapter in a book:

Because data is split into partitions, Spark can schedule one task per partition, processing multiple chunks at the same time. This improves execution speed, memory efficiency and fault tolerance. If one partition fails, Spark only needs to recompute that chunk instead of the entire dataset.

Why Partitions Are Important

Advantage

Description

Speed

Enables distributed parallel computing

Memory Efficient

Smaller chunks → easier to fit in executor memory

Fault Tolerance

If one partition fails, only that chunk needs recompute

Resource Scaling

Adaptable to small or massive workloads

How Spark Decides Number of Partitions

Spark uses configuration defaults to determine partition counts:

You can override these values at runtime to suit your workload.

Check Current Number of Partitions

To see how many partitions a DataFrame has, use the underlying RDD’s getNumPartitions():

```

from pyspark.sql import SparkSession

spark = SparkSession.builder \

.appName("CheckPartitions") \

.master("local[*]") \

.getOrCreate()

data = [("Laptop", 55000), ("Mouse", 500), ("Keyboard", 1200)]

df = spark.createDataFrame(data, ["product", "price"])

print("Partitions:", df.rdd.getNumPartitions())

```

View Data Inside Each Partition

To view the rows in each partition, use qlom() to collect the data into lists:

```

from pyspark.sql import SparkSession

spark = SparkSession.builder \

.appName("CheckPartitions") \

.master("local[*]") \

.getOrCreate()

data = [("Laptop", 55000), ("Mouse", 500), ("Keyboard", 1200)]

df = spark.createDataFrame(data, ["product", "price"])

print(df.rdd.glom().collect())

```

Adjusting Partitions

Spark offers two methods to change the number of partitions:

repartition()

```

from pyspark.sql import SparkSession

spark = SparkSession.builder \

.appName("CheckPartitions") \

.master("local[*]") \

.getOrCreate()

data = [("Laptop", 55000), ("Mouse", 500), ("Keyboard", 1200)]

df = spark.createDataFrame(data, ["product", "price"])

print(df.rdd.glom().collect())

```

Coalesce

```

from pyspark.sql import SparkSession

spark = SparkSession.builder \

.appName("CheckPartitions") \

.master("local[*]") \

.getOrCreate()

data = [("Laptop", 55000), ("Mouse", 500), ("Keyboard", 1200)]

df = spark.createDataFrame(data, ["product", "price"])

df3 = df.coalesce(1)

print(df3.rdd.getNumPartitions())

```

When to Use Each

Scenario

Recommended Method

Fix data skew before a join

repartition()

Reduce partitions after filtering

coalesce()

Prepare for heavy aggregation

repartition()

Optimize file output size

coalesce()

Defaults and Best Practices

More Spark tutorials

All tutorials · Try the free PySpark compiler · Practice challenges