Skip to content
geeksforgeeks
  • 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
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • 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:
Pandas Join Dataframes
Next article icon

How to Join Pandas DataFrames using Merge?

Last Updated : 05 May, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

Joining and merging DataFrames is that the core process to start  out with data analysis and machine learning tasks. It's one of the toolkits which each Data Analyst or Data Scientist should master because in most cases data comes from multiple sources and files. In this tutorial, you'll how to join data frames in pandas using the merge technique. More specifically, we will practice the concatenation of DataFrames along row and column.

Getting Started

The most widely used operation related to DataFrames is the merging operation. Two DataFrames might hold different kinds of information about the same entity and they may have some same columns, so we need to combine the two data frames in pandas for better reliability code. To join these DataFrames, pandas provides various functions like join(), concat(), merge(), etc. In this section, you will practice using the merge() function of pandas. There are basically four methods of merging:

  • inner join
  • outer join
  • right join
  • left join

Inner join

From the name itself, it is clear enough that the inner join keeps rows where the merge “on” value exists in both the left and right dataframes. Now let us create two dataframes and then try merging them using inner. 

Python3
import numpy as np import pandas as pd   left = pd.DataFrame({'Sr.no': ['1', '2', '3', '4', '5'],                      'Name': ['Rashmi', 'Arun', 'John',                               'Kshitu', 'Bresha'],                      'Roll No': ['1', '2', '3', '4', '5']})   right = pd.DataFrame({'Sr.no': ['2', '4', '6', '7', '8'],                      'Gender': ['F', 'M', 'M', 'F', 'F'],                      'Interest': ['Writing', 'Cricket', 'Dancing',                                  'Chess', 'Sleeping']})                           # Merging the dataframes                      pd.merge(left, right, how ='inner', on ='Sr.no')  

Output:

Outer join

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

Python3
import numpy as np import pandas as pd   left = pd.DataFrame({'Sr.no': ['1', '2', '3', '4', '5'],                      'Name': ['Rashmi', 'Arun', 'John',                              'Kshitu', 'Bresha'],                      'Roll No': ['1', '2', '3', '4', '5']})   right = pd.DataFrame({'Sr.no': ['2', '4', '6', '7', '8'],                      'Gender': ['F', 'M', 'M', 'F', 'F'],                      'Interest': ['Writing', 'Cricket', 'Dancing',                                   'Chess', 'Sleeping']})                           # Merging the dataframes                      pd.merge(left, right, how ='outer', on ='Sr.no') 

Output:

Left join

With a left 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.  

Python3
import numpy as np import pandas as pd   left = pd.DataFrame({'Sr.no': ['1', '2', '3', '4', '5'],                      'Name': ['Rashmi', 'Arun', 'John',                               'Kshitu', 'Bresha'],                      'Roll No': ['1', '2', '3', '4', '5']})   right = pd.DataFrame({'Sr.no': ['2', '4', '6', '7', '8'],                      'Gender': ['F', 'M', 'M', 'F', 'F'],                      'Interest': ['Writing', 'Cricket',                                   'Dancing', 'Chess',                                   'Sleeping']})                           # Merging the dataframes                      pd.merge(left, right, how ='left', on ='Sr.no') 

Output: Note the Output Carefully.

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

Python3
import numpy as np import pandas as pd   left = pd.DataFrame({'Sr.no': ['1', '2', '3', '4', '5'],                      'Name': ['Rashmi', 'Arun', 'John',                              'Kshitu', 'Bresha'],                      'Roll No': ['1', '2', '3', '4', '5']})   right = pd.DataFrame({'Sr.no': ['2', '4', '6', '7', '8'],                      'Gender': ['F', 'M', 'M', 'F', 'F'],                      'Interest': ['Writing', 'Cricket', 'Dancing',                                   'Chess', 'Sleeping']})                           # Merging the dataframes                      pd.merge(left, right, how ='right', on ='Sr.no')  

Output:


Next Article
Pandas Join Dataframes
author
rutujakawade24
Improve
Article Tags :
  • Python
  • Python-pandas
  • Python pandas-dataFrame
Practice Tags :
  • python

Similar Reads

  • Joining two Pandas DataFrames using merge()
    The merge() function is designed to merge two DataFrames based on one or more columns with matching values. The basic idea is to identify columns that contain common data between the DataFrames and use them to align rows. Let's understand the process of joining two pandas DataFrames using merge(), e
    4 min read
  • How to Merge Two Pandas DataFrames on Index
    Merging two pandas DataFrames on their index is necessary when working with datasets that share the same row identifiers but have different columns. The core idea is to align the rows of both DataFrames based on their indices, combining the respective columns into one unified DataFrame. To merge two
    3 min read
  • Join Pandas DataFrames matching by substring
    Prerequisites: Pandas In this article, we will learn how to join two Data Frames matching by substring with python.  Functions used:join(): joins all the elements in an iteration into a single stringlambda(): an anonymous method which is declared without a name and can accept any number of parameter
    1 min read
  • How to Union Pandas DataFrames using Concat?
    concat() function does all of the heavy liftings of performing concatenation operations along an axis while performing optional set logic (union or intersection) of the indexes (if any) on the other axes. The concat() function combines data frames in one of two ways: Stacked: Axis = 0 (This is the d
    1 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
  • 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
    4 min read
  • How to combine two DataFrames in Pandas?
    While working with data, there are multiple times when you would need to combine data from multiple sources. For example, you may have one DataFrame that contains information about a customer, while another DataFrame contains data about their transaction history. If you want to analyze this data tog
    3 min read
  • Pandas Merge Dataframe
    Merging DataFrames is a common operation when working with multiple datasets in Pandas. The `merge()` function allows you to combine two DataFrames based on a common column or index. In this article, we will explore how to merge DataFrames using various options and techniques. We will load the datas
    5 min read
  • Merge two DataFrames in PySpark
    In this article, we will learn how to merge multiple data frames row-wise in PySpark. Outside chaining unions this is the only way to do it for DataFrames. The module used is pyspark : Spark (open-source Big-Data processing engine by Apache) is a cluster computing system. It is faster as compared to
    4 min read
  • How to compare values in two Pandas Dataframes?
    Let's discuss how to compare values in the Pandas dataframe. Here are the steps for comparing values in two pandas Dataframes: Step 1 Dataframe Creation: The dataframes for the two datasets can be created using the following code:  [GFGTABS] Python3 import pandas as pd # elements of first dataset fi
    2 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