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:
Pandas Join Dataframes
Next article icon

Pandas Join Dataframes

Last Updated : 16 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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 like merge(), join(), and concat() in Pandas.

Python
import pandas as pd  data1 = {'Name': ['John', 'Alice', 'Bob', 'Eve'],         'Age': [25, 30, 22, 35],         'Gender': ['Male', 'Female', 'Male', 'Female']}  df = pd.DataFrame(data) print(df1)  data2 = {'Name': ['John', 'Alice', 'Bob', 'Charlie'],          'Salary': [50000, 55000, 40000, 48000]}  df2 = pd.DataFrame(data2) print(df2) 

We will use these datasets to demonstrate how to join DataFrames in various ways.

Joining DataFrames Using merge

The merge() function is used to combine DataFrames based on common columns or indices. It is the most flexible way to join DataFrames, offering different types of joins (inner, left, right, and outer) similar to SQL joins.

  • Use merge() to join the DataFrames based on a common column.
Python
# Merge df1 and df2 on the 'Name' column merged_df = pd.merge(df1, df2, on='Name', how='inner') print(merged_df) 
Screenshot-2024-12-13-122057
on='Name' specifies that the DataFrames will be merged based on the Name column.how='inner' performs an inner join, which only includes rows with matching values in the Name column from both DataFrames.

Performing a Left Join Using merge

A left join returns all the rows from the left DataFrame (df1) and the matching rows from the right DataFrame (df2). If no match is found, NaN values are filled for columns from the right DataFrame.

  • Use merge() with how='left' to perform a left join.
Python
# Perform a left join on 'Name' left_joined_df = pd.merge(df1, df2, on='Name', how='left') print(left_joined_df) 
Screenshot-2024-12-13-122219

how='left' ensures that all rows from the left DataFrame (df1) are included, and only the matching rows from the right DataFrame (df2) are returned.If there is no match in df2, the Salary column will have NaN for that row.

Performing a Right Join Using merge

A right join returns all rows from the right DataFrame (df2) and the matching rows from the left DataFrame (df1). If no match is found, NaN values are filled for columns from the left DataFrame.

  • Use merge() with how='right' to perform a right join.
Python
# Perform a right join on 'Name' right_joined_df = pd.merge(df1, df2, on='Name', how='right') print(right_joined_df) 
Screenshot-2024-12-13-122423

how='right' ensures that all rows from the right DataFrame (df2) are included, and only the matching rows from the left DataFrame (df1) are returned.If there is no match in df1, the columns from df1 will have NaN.

Performing an Outer Join Using merge

An outer join returns all rows from both DataFrames. If a row in one DataFrame has no match in the other, NaN values are filled for the missing values.

  • Use merge() with how='outer' to perform an outer join.
Python
# Perform an outer join on 'Name' outer_joined_df = pd.merge(df1, df2, on='Name', how='outer') print(outer_joined_df) 
Screenshot-2024-12-13-122536

Joining DataFrames Using join

The join() method is another way to combine DataFrames, but it works by using the index of the DataFrames, not columns. It is often used when you have a DataFrame with a meaningful index and want to join another DataFrame based on that index.

  • Use join() to join DataFrames based on the index.
Python
# Set 'Name' as the index for both DataFrames df1.set_index('Name', inplace=True) df2.set_index('Name', inplace=True)  # Join df1 with df2 on the index joined_df = df1.join(df2) print(joined_df) 
Screenshot-2024-12-13-122747

The join() method merges DataFrames using their indexes. By setting the Name column as the index, we can join the DataFrames based on the index values.

Concatenating DataFrames Using concat

The concat() method allows you to concatenate DataFrames either vertically (along rows) or horizontally (along columns). This is different from a SQL-style join and is useful when you want to combine DataFrames along a particular axis.

Python
# Concatenate df1 and df2 along rows (vertical concatenation) concatenated_df = pd.concat([df1, df2], axis=0) print(concatenated_df) 
Screenshot-2024-12-13-122913

The concat() method concatenates DataFrames along a particular axis. Setting axis=0 combines them along rows (vertical concatenation), while axis=1 would concatenate along columns (horizontal concatenation).


Summary:

Joining DataFrames is an essential operation in data analysis. Pandas provides flexible methods for combining DataFrames, including:

  • merge(): Allows you to perform SQL-like joins (inner, left, right, outer).
  • join(): Joins DataFrames based on their indexes.
  • concat(): Concatenates DataFrames along rows or columns.

By understanding and using these methods, you can efficiently combine data from multiple sources to perform more complex analyses.

Related Articles:

  • Python | Pandas Merging, Joining, and Concatenating
  • Joining two Pandas DataFrames using merge

Next Article
Pandas Join Dataframes

A

abhirajksingh
Improve
Article Tags :
  • Python
  • Pandas
  • AI-ML-DS
  • Python-pandas
  • Python pandas-basics
  • Python pandas-dataFrame
  • Python pandas-io
Practice Tags :
  • python

Similar Reads

    Pandas DataFrame
    A Pandas DataFrame is a two-dimensional table-like structure in Python where data is arranged in rows and columns. It’s one of the most commonly used tools for handling data and makes it easy to organize, analyze and manipulate data. It can store different types of data such as numbers, text and dat
    10 min read
    Pandas Dataframe Difference
    When working with multiple DataFrames, you might want to compute the differences between them, such as identifying rows that are in one DataFrame but not in another. Pandas provides various ways to compute the difference between DataFrames, whether it's comparing rows, columns, or entire DataFrames.
    4 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 datase
    5 min read
    Merge Multiple Dataframes - Pandas
    Merging allow us to combine data from two or more DataFrames into one based on index values. This is used when we want to bring together related information from different sources. In Pandas there are different ways to combine DataFrames:1. Merging DataFrames Using merge()We use merge() when we want
    3 min read
    Pandas Combine Dataframe
    Combining DataFrames in Pandas is a fundamental operation that allows users to merge, concatenate, or join data from multiple sources into a single DataFrame. This article explores the different techniques we can use to combine DataFrames in Pandas, focusing on concatenation, merging and joining.Pyt
    3 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