home
diamond Go Premium
Data Engineering Path  ·  PySpark

DataFrame Partitioning: Repartition vs Coalesce

Managing performance and execution parallelisms in PySpark by controlling partition sizing and data layouts.


What is Partition Management in PySpark?

Distributed DataFrames are split horizontally into discrete data chunks called partitions. Partitions determine execution parallelisms—each executor core processes exactly one partition at a time.

PySpark offers two operations to adjust the partition layout:

  1. repartition(numPartitions, columns...): Resizes partitions via a full shuffle across nodes. Used to increase or decrease partitions, or align data by a grouping key.
  2. coalesce(numPartitions): Decreases partitions without a shuffle by merging adjacent local partitions on the same node. It is highly optimized and fast, but cannot increase partitioning.

Architectural Comparison: Repartition vs. Coalesce

graph TD
    subgraph Repartition Shuffle
        A1["Node A (P1)"] -.-> B1["Node A (New P1)"]
        A1 -.-> B2["Node B (New P2)"]
        A2["Node B (P2)"] -.-> B1
        A2 -.-> B2
    end
    subgraph Coalesce Merge
        C1["Node A (P1)"] --> D1["Node A (Merged P1)"]
        C2["Node A (P2)"] --> D1
    end
Aspect repartition() coalesce()
Shuffle Overhead Yes (full hash shuffle). Heavy network I/O. No shuffle (node-local merge). Extremely fast.
Partition Count Can increase or decrease partition count. Can only decrease partition count.
Data Distribution Uniformly balances data size across nodes. Can cause data skew if local partitions merge unevenly.
Best For Parallelizing tiny datasets or grouping data by join key. Reducing partitions before writing output files to disk.

Example Usage Pipeline

Below is a complete, copy-paste-ready PySpark script demonstrating partitioning and inspection:

from pyspark.sql import SparkSession

# 1. Setup local Spark session
spark = SparkSession.builder \
    .appName("DataFrame Partitioning Demo") \
    .master("local[*]") \
    .getOrCreate()

# 2. Generate a DataFrame from dummy data
data = [(x, f"User_{x}") for x in range(1, 1001)]
df = spark.createDataFrame(data, ["id", "name"])

print(f"=== Initial Partition Count: {df.rdd.getNumPartitions()} ===")

# 3. Increase partition count to 8 using repartition() (full shuffle)
repartitioned_df = df.repartition(8)
print(f"=== Count after repartition(8): {repartitioned_df.rdd.getNumPartitions()} ===")

# 4. Decrease partition count down to 2 using coalesce() (optimized, no shuffle)
coalesced_df = repartitioned_df.coalesce(2)
print(f"=== Count after coalesce(2): {coalesced_df.rdd.getNumPartitions()} ===")

# 5. Repartition by column key (hash-partitioning by specific field)
# Groups all identical values of 'id' into the same partition (ideal before heavy joins)
keyed_partition_df = df.repartition(4, "id")
print(f"=== Count after keyed repartition: {keyed_partition_df.rdd.getNumPartitions()} ===")

Rendered Output:

=== Initial Partition Count: 10 ===
=== Count after repartition(8): 8 ===
=== Count after coalesce(2): 2 ===
=== Count after keyed repartition: 4 ===
Find this content helpful? ☕ Buy me a coffee

Entity Details

Create New Item

celebration
Enjoying the free content?

Create a free account to track your progress and save your place.

Create Free Account
help

Submit Technical Query

Have a question or run into an issue? Describe it below, upload an optional screenshot, and our engineering team will answer it!

image Attach image (optional)

Submit Feedback

build Free Developer Utility Free Tool
gavel

Privacy & Legal Disclaimer

1. Client-Side Browser Processing

All utility tools on DeepEngineerHub (including Image to PDF, Text Formatters, JSON Converters, and Encryptors) execute 100% locally within your client browser using WebAssembly and JavaScript. No uploaded images, text, or documents are transmitted, collected, or stored on remote servers.

2. Limitation of Liability ("As-Is" Provision)

Tools and services are provided free of charge for convenience and educational purposes "as-is" without warranties of any kind. DeepEngineerHub shall not be held liable for any data loss, formatting inconsistencies, or indirect damages resulting from tool usage.

3. Open Source & Third-Party Software

Certain utilities utilize open-source client libraries (such as jsPDF, Mermaid.js, Pyodide) licensed under MIT, Apache, or BSD open licenses. All intellectual property remains with their respective copyright holders.