Different Types of Joins in Pandas



Different Types of Joins in Pandas

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 mainly 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:

# 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:

# 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:

 

We will use these two dataframes to understand the different types of joins.

 

  • 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:

# 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:

  • Left Outer 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:

# 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:

 

  • 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:

# 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:

 

  • Full Outer Join: A full outer join returns all the rows from the left dataframe, 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:

# 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:

 

  • 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.

Example:

# 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:

 

Last Updated on October 21, 2021 by admin

Leave a Reply

Your email address will not be published. Required fields are marked *

Recommended Blogs