Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Data preprocessing
  • Data Manipulation
  • Data Analysis using Pandas
  • EDA
  • Pandas Exercise
  • Pandas AI
  • Numpy
  • Matplotlib
  • Plotly
  • Data Analysis
  • Machine Learning
  • Data science
Open In App
Next Article:
Different Types of Joins in Pandas
Next article icon

Different Types of Joins in Pandas

Last Updated : 28 Aug, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

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.

Pandas Inner Join
 

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.

left-joinExample:

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.

Pandas Right Outer Join

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.

Pandas Full Outer Join

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:


Next Article
Different Types of Joins in Pandas

P

pawki
Improve
Article Tags :
  • Technical Scripter
  • Python
  • Technical Scripter 2020
  • Python-pandas
Practice Tags :
  • python

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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences