Featured Article

Joins in PySpark with Examples: Master DataFrame Merges Today

Kenneth Jul 13, 2026

In the realm of big data processing, Apache Spark's PySpark library provides a powerful interface for performing operations on data in parallel. One crucial aspect of data manipulation in PySpark is the ability to join DataFrames or DataSets, an operation that allows you to combine data from two or more sources based on a related column. Let's delve into the world of joins in PySpark, exploring the different types and providing practical examples.

PySpark Join Two or Multiple DataFrames
PySpark Join Two or Multiple DataFrames

Before we dive into the types of joins, it's essential to grasp the basic syntax. PySpark provides the join method for DataFrames and DataSets, which takes the following syntax: df1.join(df2, on=['column1', 'column2'], how='inner'). Here, df1 and df2 are the DataFrames to be joined, 'column1' and 'column2' are the columns for joining, and 'inner' specifies the type of join.

The Benefits & Examples of Using Apache Spark with PySpark | Exxact Blog
The Benefits & Examples of Using Apache Spark with PySpark | Exxact Blog

Types of Joins in PySpark

The type of join determines which elements are retained in the resulting DataFrame. There are several types of joins in PySpark, each serving a different purpose in data analysis.

#tcs #dataengineer #dataengineering #pyspark #azuredatafactory #deltalake #sql #bigdata #interviewpreparation #careergrowth #techcareers | Kushboo Chauhan
#tcs #dataengineer #dataengineering #pyspark #azuredatafactory #deltalake #sql #bigdata #interviewpreparation #careergrowth #techcareers | Kushboo Chauhan

To understand joins better, let's consider two DataFrames, df1 and df2, with the following data:

  • df1: [ID, Name, Age]
  • df2: [ID, City, Salary]
Timber Frame Joinery Archives
Timber Frame Joinery Archives

Inner Join

The inner join retains only the rows where there is a match in both DataFrames on the specified join column(s). In our example, an inner join on ID would return:

  • ID: 1, Name: John, City: New York, Salary: 50000, Age: 30
  • ID: 2, Name: Jane, City: Boston, Salary: 60000, Age: 25
SQL Cheat Sheet for Beginners
SQL Cheat Sheet for Beginners

Left Outer Join (Left Join)

In a left join, all rows from the first DataFrame (df1) are included, with matched rows from the second DataFrame (df2). If there's no match, the result is NULL on the right side. Here's what it looks like:

  • ID: 1, Name: John, City: New York, Salary: 50000, Age: 30
  • ID: 2, Name: Jane, City: Boston, Salary: 60000, Age: 25
  • ID: 3, Name: Bob, Age: 28, City: NULL, Salary: NULL
Wood Joinery Guide
Wood Joinery Guide

Right Outer Join (Right Join)

In contrast to the left join, the right join includes all rows from df2 and matches them against df1. If there's no match, the result is NULL on the left side:

Annaplexis
Annaplexis
Joining Wood
Joining Wood
Marketplace Pitch Deck - How To Instructions
Marketplace Pitch Deck - How To Instructions
Mastering the Craft: Exploring Types of Joineries in Woodworking
Mastering the Craft: Exploring Types of Joineries in Woodworking
the team strategy system is shown in black and white, with different symbols on it
the team strategy system is shown in black and white, with different symbols on it
Simple Way to Join Knitted Squares and Panels to Make Blankets and Other Reversible Projects - 10 rows a day
Simple Way to Join Knitted Squares and Panels to Make Blankets and Other Reversible Projects - 10 rows a day
Joints
Joints
🔗 Django Models – Relationship Fields
🔗 Django Models – Relationship Fields
a man in a business suit holding a clipboard next to a laptop and money
a man in a business suit holding a clipboard next to a laptop and money
  • ID: 2, Name: Jane, City: Boston, Salary: 60000, Age: 25
  • ID: 3, City: Los Angeles, Salary: 70000, Name: NULL, Age: NULL

Full Outer Join (Full Join)

The full outer join combines all the above, keeping all rows from both DataFrames and filling in NULLs where necessary:

  • ID: 1, Name: John, City: New York, Salary: 50000, Age: 30
  • ID: 2, Name: Jane, City: Boston, Salary: 60000, Age: 25
  • ID: 3, Name: Bob, Age: 28, City: Los Angeles, Salary: 70000

Spatial Joins

PySpark also supports spatial joins, which are useful when working with spatial data. They allow you to combine data based on spatial relationships, such as 'intersects', 'contains', etc. However, spatial joins are more complex and require a dedicated module.

Now that you've seen the different types of joins in action, you're well-equipped to tackle data manipulation tasks in PySpark. The world of big data awaits, filled with insights to be uncovered through strategic joins. Happy coding!