Different Types of Joins in Pandas
Last Updated : 28 Aug, 2023
The Pandas module contains various features to perform various operations on Dataframes like join, concatenate, delete, add, etc. In this article, we are going to discuss the various types of join operations that can be performed on Pandas Dataframe. There are five types of Joins in Pandas.
- Inner Join
- Left Outer Join
- Right Outer Join
- Full Outer Join or simply Outer Join
- Index Join
To understand different types of joins, we will first make two DataFrames, namely a and b.
Dataframe a:
Python3 # importing pandas import pandas as pd # Creating dataframe a a = pd.DataFrame() # Creating Dictionary d = {'id': [1, 2, 10, 12], 'val1': ['a', 'b', 'c', 'd']} a = pd.DataFrame(d) # printing the dataframe a
Output:
DataFrame b:
Python3 # importing pandas import pandas as pd # Creating dataframe b b = pd.DataFrame() # Creating dictionary d = {'id': [1, 2, 9, 8], 'val1': ['p', 'q', 'r', 's']} b = pd.DataFrame(d) # printing the dataframe b
Output:
Types of Joins in Pandas
We will use these two Dataframes to understand the different types of joins.
Pandas Inner Join
Inner join is the most common type of join you’ll be working with. It returns a Dataframe with only those rows that have common characteristics. This is similar to the intersection of two sets.
Example:
Python3 # importing pandas import pandas as pd # Creating dataframe a a = pd.DataFrame() # Creating Dictionary d = {'id': [1, 2, 10, 12], 'val1': ['a', 'b', 'c', 'd']} a = pd.DataFrame(d) # Creating dataframe b b = pd.DataFrame() # Creating dictionary d = {'id': [1, 2, 9, 8], 'val1': ['p', 'q', 'r', 's']} b = pd.DataFrame(d) # inner join df = pd.merge(a, b, on='id', how='inner') # display dataframe df
Output:

Pandas Left Join
With a left outer join, all the records from the first Dataframe will be displayed, irrespective of whether the keys in the first Dataframe can be found in the second Dataframe. Whereas, for the second Dataframe, only the records with the keys in the second Dataframe that can be found in the first Dataframe will be displayed.
Example:
Python3 # importing pandas import pandas as pd # Creating dataframe a a = pd.DataFrame() # Creating Dictionary d = {'id': [1, 2, 10, 12], 'val1': ['a', 'b', 'c', 'd']} a = pd.DataFrame(d) # Creating dataframe b b = pd.DataFrame() # Creating dictionary d = {'id': [1, 2, 9, 8], 'val1': ['p', 'q', 'r', 's']} b = pd.DataFrame(d) # left outer join df = pd.merge(a, b, on='id', how='left') # display dataframe df
Output:

Pandas Right Outer Join
For a right join, all the records from the second Dataframe will be displayed. However, only the records with the keys in the first Dataframe that can be found in the second Dataframe will be displayed.

Example:
Python3 # importing pandas import pandas as pd # Creating dataframe a a = pd.DataFrame() # Creating Dictionary d = {'id': [1, 2, 10, 12], 'val1': ['a', 'b', 'c', 'd']} a = pd.DataFrame(d) # Creating dataframe b b = pd.DataFrame() # Creating dictionary d = {'id': [1, 2, 9, 8], 'val1': ['p', 'q', 'r', 's']} b = pd.DataFrame(d) # right outer join df = pd.merge(a, b, on='id', how='right') # display dataframe df
Output:

Pandas Full Outer Join
A full outer join returns all the rows from the left Dataframe, and all the rows from the right Dataframe, and matches up rows where possible, with NaNs elsewhere. But if the Dataframe is complete, then we get the same output.

Example:
Python3 # importing pandas import pandas as pd # Creating dataframe a a = pd.DataFrame() # Creating Dictionary d = {'id': [1, 2, 10, 12], 'val1': ['a', 'b', 'c', 'd']} a = pd.DataFrame(d) # Creating dataframe b b = pd.DataFrame() # Creating dictionary d = {'id': [1, 2, 9, 8], 'val1': ['p', 'q', 'r', 's']} b = pd.DataFrame(d) # full outer join df = pd.merge(a, b, on='id', how='outer') # display dataframe df
Output:

Pandas Index Join
To merge the Dataframe on indices pass the left_index and right_index arguments as True i.e. both the Dataframes are merged on an index using default Inner Join.
Python3 # importing pandas import pandas as pd # Creating dataframe a a = pd.DataFrame() # Creating Dictionary d = {'id': [1, 2, 10, 12], 'val1': ['a', 'b', 'c', 'd']} a = pd.DataFrame(d) # Creating dataframe b b = pd.DataFrame() # Creating dictionary d = {'id': [1, 2, 9, 8], 'val1': ['p', 'q', 'r', 's']} b = pd.DataFrame(d) # index join df = pd.merge(a, b, left_index=True, right_index=True) # display dataframe df
Output:
Similar Reads
What is the difference between join and merge in Pandas? In Pandas, join() combines DataFrames based on their indices and defaults to a left join, while merge() joins on specified columns and defaults to an inner join. Choosing the right method depends on how your data is aligned. To illustrate the difference between join() and merge() visually, Let's und
4 min read
Python Pandas - Difference between INNER JOIN and LEFT SEMI JOIN In this article, we see the difference between INNER JOIN and LEFT SEMI JOIN. Inner Join An inner join requires two data set columns to be the same to fetch the common row data values or data from the data table. In simple words, and returns a data frame or values with only those rows in the data fr
3 min read
How to Merge DataFrames of different length in Pandas ? Merging DataFrames of different lengths in Pandas can be done using the merge(), and concat(). These functions allow you to combine data based on shared columns or indices, even if the DataFrames have unequal lengths. By using the appropriate merge method (like a left join, right join, or outer join
3 min read
PySpark Join Types - Join Two DataFrames In PySpark, joins combine rows from two DataFrames using a common key. Common types include inner, left, right, full outer, left semi and left anti joins. Each type serves a different purpose for handling matched or unmatched data during merges. The syntax is:dataframe1.join(dataframe2,dataframe1.co
9 min read
Pandas Join Dataframes Joining DataFrames is a common operation in data analysis, where you combine two or more DataFrames based on common columns or indices. Pandas provides various methods to perform joins, allowing you to merge data in flexible ways. In this article, we will explore how to join DataFrames using methods
4 min read