Joins in PySpark DataFrames
Joins are one of the most common operations when working with data.
In PySpark, joins let you combine DataFrames based on matching keys β while taking advantage of Sparkβs distributed computing engine.
This guide covers all join types in PySpark, including:
- Inner Join
- Left Join
- Right Join
- Full Outer Join
- Left Semi Join
- Left Anti Join
- Cross Join
Each join comes with a clean explanation, Python code, and output.
Load Example Datasetsβ
from pyspark.sql import SparkSession
spark = SparkSession.builder.appName('joins').getOrCreate()
# Authors dataset
authors = [(1, "John Smith"),
(2, "Jane Doe"),
(3, "Alex Lee"),
(4, "Anna Ray")]
df_authors = spark.createDataFrame(authors, ["AuthorID", "Author"])
# Books dataset
books = [(1, "Deep Learning"),
(2, "AI Ethics"),
(5, "Data Science")]
df_books = spark.createDataFrame(books, ["AuthorID", "Book"])
1. Inner Join
df_authors.join(df_books, on="AuthorID", how="inner").show()
π When to Use:β
Use inner join when you only need rows that exist in both DataFrames.
Example: authors who actually wrote a book.
β Resultβ
| AuthorID | Author | Book |
|---|---|---|
| 1 | John Smith | Deep Learning |
| 2 | Jane Doe | AI Ethics |
2. Left Join (Left Outer Join)
df_authors.join(df_books, on="AuthorID", how="left").show()
π When to Use:β
Use when you want all authors, even if they donβt have a book.
β Resultβ
| AuthorID | Author | Book |
|---|---|---|
| 1 | John Smith | Deep Learning |
| 2 | Jane Doe | AI Ethics |
| 3 | Alex Lee | null |
| 4 | Anna Ray | null |
3. Right Join (Right Outer Join)
df_authors.join(df_books, on="AuthorID", how="right").show()
π When to Use:β
Use when you want all books, even if no author matches.
β Resultβ
| AuthorID | Author | Book |
|---|---|---|
| 1 | John Smith | Deep Learning |
| 2 | Jane Doe | AI Ethics |
| 5 | null | Data Science |
4. Full Outer Join
df_authors.join(df_books, on="AuthorID", how="outer").show()
π When to Use:β
Use this join when you want all records from both sides, matched where possible.
β Resultβ
| AuthorID | Author | Book |
|---|---|---|
| 1 | John Smith | Deep Learning |
| 2 | Jane Doe | AI Ethics |
| 3 | Alex Lee | null |
| 4 | Anna Ray | null |
| 5 | null | Data Science |
5. Left Semi Join
df_authors.join(df_books, on="AuthorID", how="left_semi").show()
π When to Use:β
Use left semi join when you only need rows from the left DataFrame that have a match in the right.
Note: You only get left columns β right DataFrame columns are removed.
Example:β
Find authors who have written a book.
β Resultβ
| AuthorID | Author |
|---|---|
| 1 | John Smith |
| 2 | Jane Doe |
6. Left Anti Join
df_authors.join(df_books, on="AuthorID", how="left_anti").show()
π When to Use:β
Use when you want rows from the left DataFrame that do NOT exist in the right.
Example:β
Find authors without any published book.
β Resultβ
| AuthorID | Author |
|---|---|
| 3 | Alex Lee |
| 4 | Anna Ray |
7. Cross Join (Cartesian Product)
df_authors.crossJoin(df_books).show()