Featured Article

Master Joins PySpark DataFrame Like a Pro

Kenneth Jul 13, 2026

Working with big data in Python often involves using the PySpark library, which is a powerful tool for processing large datasets in a distributed manner. One of the fundamental tasks when working with PySpark is combining or joining DataFrames, as it's essential for performing complex data analysis and transformations. In this article, we'll delve into the process of joining PySpark DataFrames, exploring the different types of joins and providing practical examples.

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

Before diving into the details of joins, let's recap what PySpark DataFrames are. PySpark DataFrames, similar to traditional dataframes, are two-dimensional, sized-based data structures that allow you to manipulate and analyze structured data efficiently. They are built upon Resilient Distributed Datasets (RDDs), enabling distributed computing capabilities.

How to Display a PySpark DataFrame in Table Format | Towards Data Science
How to Display a PySpark DataFrame in Table Format | Towards Data Science

Types of Joins in PySpark

The first step in understanding how to join PySpark DataFrames is knowing the types of joins available. PySpark provides four types of joins: inner join, left outer join, right outer join, and full outer join. Each of these joins differs in its behavior when matching or accommodating non-matching keys.

an image of a black and red abstract background with squares in the center, as well as text
an image of a black and red abstract background with squares in the center, as well as text

Let's explore these join types and their key differences.

Inner Join

an image of some diagrams on the side of a black background with orange and white details
an image of some diagrams on the side of a black background with orange and white details

An inner join returns only the rows where there is a match in both DataFrames on the specified join keys. In other words, it retains only the intersecting data between the two DataFrames. Inner joins are the most common and performant type of join.

Here's a simple example of an inner join in PySpark:

result = df1.join(df2, df1.key == df2.key, 'inner')

Here's a souvenir for you Data people 📊
Here's a souvenir for you Data people 📊

Left Outer Join

In a left outer join, the resulting DataFrame includes all the rows from the left DataFrame (df1) and the matching rows from the right DataFrame (df2) based on the join keys. If there is no match, the result is `null` for the right DataFrame's columns.

To perform a left outer join:

Features Section
Features Section

result = df1.join(df2, df1.key == df2.key, 'left_outer')

Performing Joins with Conditions

an image of some dots that are in the shape of numbers
an image of some dots that are in the shape of numbers
Hadoop vs Spark data processing
Hadoop vs Spark data processing
the dashboard screen shows several different types of data and information, including numbers, bars, and graphs
the dashboard screen shows several different types of data and information, including numbers, bars, and graphs
the data pipeline is shown in green and white, with icons above it on top
the data pipeline is shown in green and white, with icons above it on top
an info sheet showing the number of different types of items that are available for purchase
an info sheet showing the number of different types of items that are available for purchase
the flow diagram shows how to use an appliance for storage and data processing
the flow diagram shows how to use an appliance for storage and data processing
an image of some type of black and white background with different lines in the middle
an image of some type of black and white background with different lines in the middle
an abstract circle consisting of dots and lines in green, blue and grey colors on a white background
an abstract circle consisting of dots and lines in green, blue and grey colors on a white background
an abstract background with circles and dots in blue, red and grey colors on a light beige background
an abstract background with circles and dots in blue, red and grey colors on a light beige background

So far, we've explored the basic syntax of joining DataFrames using equality (`==`) as the condition. However, PySpark DataFrames support more complex join conditions with logical operators and functions. This flexibility enables you to perform joins based on more intricate rules.

Let's consider an example where we want to join two DataFrames on a condition involving a range of values:

result = df1.join(df2, (df1.value > df2.min_value) & (df1.value < df2.max_value), 'left_semi')

Left Semi Join

Before concluding this article, let's briefly discuss the left semi join, which might not be as well-known but is still a useful join type. A left semi join returns only the rows from the left DataFrame where a match is found in the right DataFrame for the specified join keys.

Here's the syntax for a left semi join:

result = df1.join(df2, on='key', how='left_semi')

Now that you've explored the various types of joins and their application, you're equipped to handle a wide range of data manipulation tasks in PySpark. Keep practicing and experimenting with different join scenarios to hone your proficiency in using PySpark DataFrames.