Data transformation is a critical step in working with PySpark to manipulate and prepare large-scale datasets. In this blog, we will explore two essential PySpark transformation commands: df.union() and df.repartition(). We’ll cover their purpose, how to use them, and various scenarios where these commands shine in real-world projects.
What is df.union()?

The union() method is used to combine two DataFrames in PySpark, resulting in a single DataFrame. This is particularly helpful when you want to consolidate data from multiple sources or process different partitions as a single dataset.
Key Points:
- The DataFrames being combined must have the same schema (same number and type of columns).
- The union operation does not remove duplicate rows by default; you can use .distinct()to achieve that if needed.
Example Usage:
from pyspark.sql import SparkSession
# Initialize Spark Session
spark = SparkSession.builder.appName("UnionExample").getOrCreate()
# Create two DataFrames
data1 = [(1, "Alice"), (2, "Bob")]
data2 = [(3, "Charlie"), (4, "David")]
df1 = spark.createDataFrame(data1, ["id", "name"])
df2 = spark.createDataFrame(data2, ["id", "name"])
# Combine DataFrames using union()
result_df = df1.union(df2)
# Show the result
result_df.show()Output:
| id|   name|
|  1|  Alice|
|  2|    Bob|
|  3|Charlie|
|  4|  David|
Common Scenarios for Using df.union()
1. Combining Datasets from Multiple Sources
If your data resides in different files or partitions, you can use union() to merge them into one:
file1_df = spark.read.csv("data1.csv", header=True)
file2_df = spark.read.csv("data2.csv", header=True)
# Combine the data
merged_df = file1_df.union(file2_df)
2. Appending Data to an Existing Dataset
Suppose you want to add new data to an existing DataFrame:
new_data = [(5, "Eve")]
new_df = spark.createDataFrame(new_data, ["id", "name"])
updated_df = result_df.union(new_df)
What is df.repartition()?

The repartition() method is used to redistribute the data across a specified number of partitions in a DataFrame. It is an essential tool for performance optimization, especially when dealing with skewed data or preparing the data for parallel processing.
Key Points:
- df.repartition()increases or decreases the number of partitions.
- It ensures uniform data distribution by shuffling data across the cluster.
- For better efficiency, use repartition()overcoalesce()when increasing partitions.
Example Usage:
# Repartition the DataFrame into 4 partitions
repartitioned_df = result_df.repartition(4)
# Check the number of partitions
print(f"Number of partitions: {repartitioned_df.rdd.getNumPartitions()}")
Common Scenarios for Using df.repartition()
1. Preparing for Parallel Processing
Repartitioning your data ensures it is evenly distributed across executors, which is crucial for parallel processing in PySpark.
# Optimize for parallel processing
df = df.repartition(8)
2. Addressing Skewed Data
If one partition contains significantly more data than others, it can cause performance bottlenecks. Use repartition() to balance the workload:
# Balance skewed data
balanced_df = df.repartition(10)
3. Optimizing Data Storage
When saving data to storage (e.g., S3, HDFS), you might want to control the number of output files:
# Save with specific partition count
df.repartition(5).write.mode("overwrite").parquet("output_path")
Combining Both Commands in a Workflow
Let’s combine both commands in a real-world scenario: merging datasets and optimizing their partitioning before saving.
# Load datasets
df1 = spark.read.csv("data1.csv", header=True)
df2 = spark.read.csv("data2.csv", header=True)
# Combine datasets
merged_df = df1.union(df2)
# Optimize partitions
final_df = merged_df.repartition(10)
# Save the optimized dataset
final_df.write.mode("overwrite").parquet("final_output")
Best Practices and Tips
- Schema Consistency: Ensure the schemas of the DataFrames match before using union().
- Partition Count: Choose an appropriate number of partitions based on the dataset size and cluster configuration.
- Avoid Overuse: Excessive repartitioning can lead to performance overhead due to data shuffling.
- Debugging: Use .explain()to understand how your transformations affect the execution plan.
PySpark’s df.union() and df.repartition() are powerful tools for transforming and optimizing data. By understanding their use cases and implementing them effectively, you can build scalable and efficient data pipelines tailored to your project’s needs.
External Link: Official PySpark Documentation
 
				 
 