How to re-partition pyspark dataframe?
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Repartitioning a PySpark DataFrame changes how rows are distributed across Spark partitions. You usually do it to increase parallelism, reduce tiny output files, or distribute data by a key before a join or aggregation.
Check the Current Partition Count
Before changing anything, inspect the current number of partitions:
That tells you how many partitions back the DataFrame right now. It does not tell you whether the distribution is balanced, but it is the first thing to check.
Use repartition() When You Need a Shuffle
repartition() creates a new DataFrame with a new partition layout. It performs a shuffle, so rows can move across the cluster.
To set an explicit number of partitions:
This is useful when the current partition count is too low for the cluster or when an upstream operation left you with a poor layout.
You can also repartition by a column:
That tells Spark to hash-partition the rows using region. It is often helpful before joins or aggregations on the same key because related rows end up together more predictably.
Use coalesce() When Reducing Partitions
If your goal is only to reduce the number of partitions, coalesce() is often cheaper:
Unlike repartition(), coalesce() tries to avoid a full shuffle. That makes it a better option when you filtered the data down and now want fewer output files.
A common write pattern looks like this:
That can be useful for tiny datasets or demos, but be careful with coalesce(1) in production. For large data, forcing everything into one partition creates a bottleneck.
Repartition for Joins and Aggregations
Partitioning strategy matters most around shuffle-heavy operations.
Suppose two large DataFrames are joined on customer_id. Repartitioning both by that key can improve distribution:
This is not a universal rule. Spark's optimizer may choose a broadcast join or another plan that makes manual repartitioning unnecessary. Still, when you are working with large, similarly sized tables and a clear join key, repartitioning by that key is a standard tool.
Repartition Before Writing Output
Output layout is another common reason to repartition.
If a DataFrame has 500 partitions and you write it to disk, you may create roughly 500 part files. Sometimes that is fine. Sometimes it is a maintenance problem.
To reduce file count:
Choose the number based on data size, downstream expectations, and cluster resources. There is no magic number that fits every workload.
Common Pitfalls
The biggest mistake is using repartition() casually. It triggers a shuffle, and shuffles are expensive. If you only need fewer partitions, prefer coalesce().
Another common issue is picking a partition key with heavy skew. If one value appears far more often than others, one partition may become much larger and slower than the rest.
People also overuse coalesce(1) to get a single output file. That works for small data, but it destroys parallelism and can make large jobs painfully slow.
Finally, checking only the partition count is not enough. A DataFrame can have the "right" number of partitions and still be poorly balanced. Inspect the execution plan and job behavior when performance matters.
Summary
- Use
df.rdd.getNumPartitions()to inspect the current partition count. - Use
repartition()when you need a new distribution or partitioning by key. - Use
coalesce()when reducing partitions without a full shuffle. - Repartitioning is especially relevant before joins, aggregations, and writes.
- Avoid unnecessary shuffles and be careful with skewed keys or
coalesce(1).
Related reading
- How to read a csv file from an s3 bucket using Pandas in Python
- How to read a Parquet file into Pandas DataFrame?
- How to read a .xlsx file using the pandas Library in iPython?
- How to read binary files in Python using NumPy?
- How to read data using key in Kafka Consumer API?
- How to read json data using scala from kafka topic in apache spark
- How to read data from numpy files in TensorFlow?
- How to recreate same DocumentTermMatrix with new test data

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.