Open In App

Joining two Pandas DataFrames using merge()

Last Updated : 17 Aug, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

Let us see how to join two Pandas DataFrames using the merge() function.

merge()

Syntax : DataFrame.merge(parameters)

Parameters :

  • right : DataFrame or named Series
  • how : {‘left’, ‘right’, ‘outer’, ‘inner’}, default ‘inner’
  • on : label or list
  • left_on : label or list, or array-like
  • right_on : label or list, or array-like
  • left_index : bool, default False
  • right_index : bool, default False
  • sort : bool, default False
  • suffixes : tuple of (str, str), default (‘_x’, ‘_y’)
  • copy : bool, default True
  • indicator : bool or str, default False
  • validate : str, optional

Returns : A DataFrame of the two merged objects.

Example 1 : Merging two Dataframe with same number of elements :




# importing the module
import pandas as pd
  
# creating the first DataFrame
df1 = pd.DataFrame({"fruit" : ["apple", "banana", "avocado"],
                    "market_price" : [21, 14, 35]})
display("The first DataFrame")
display(df1)
  
# creating the second DataFrame
df2 = pd.DataFrame({"fruit" : ["banana", "apple", "avocado"],
                    "wholesaler_price" : [65, 68, 75]})
display("The second DataFrame")
display(df2)
  
# joining the DataFrames
display("The merged DataFrame")
pd.merge(df1, df2, on = "fruit", how = "inner")


Output :

Example 2 : Merging two Dataframe with different number of elements :




# importing the module
import pandas as pd
  
# creating the first DataFrame
df1 = pd.DataFrame({"fruit" : ["apple", "banana"
                               "avocado", "grape"],
                    "market_price" : [21, 14, 35, 38]})
display("The first DataFrame")
display(df1)
  
# creating the second DataFrame
df2 = pd.DataFrame({"fruit" : ["apple", "banana", "grape"],
                    "wholesaler_price" : [65, 68, 71]})
display("The second DataFrame")
display(df2)
  
# joining the DataFrames
# here both common DataFrame elements are in df1 and df2, 
# so it extracts apple, banana, grapes from df1 and df2.  
display("The merged DataFrame")
pd.merge(df1, df2, on = "fruit", how = "inner")


Output :

If we use how = "Outer", it returns all elements in df1 and df2 but if element column are null then its return NaN value.




pd.merge(df1, df2, on = "fruit", how = "outer")


Output :

If we use how = "left", it returns all the elements that present in the left DataFrame.




pd.merge(df1, df2, on = "fruit", how = "left")


Output :

If we use how = "right", it returns all the elements that present in the right DataFrame.




pd.merge(df1, df2, on = "fruit", how = "right")


Output :



Previous Article
Next Article

Similar Reads

Prevent duplicated columns when joining two Pandas DataFrames
Column duplication usually occurs when the two data frames have columns with the same name and when the columns are not used in the JOIN statement. In this article, let us discuss the three different methods in which we can prevent duplication of columns when joining two data frames. Syntax: pandas.merge(left, right, how='inner', on=None, left_on=N
5 min read
Pandas - Merge two dataframes with different columns
Pandas support three kinds of data structures. They are Series, Data Frame, and Panel. A Data frame is a two-dimensional data structure, Here data is stored in a tabular format which is in rows and columns. We can create a data frame in many ways. Here we are creating a data frame using a list data structure in python. C/C++ Code # import required
3 min read
Merge two Pandas dataframes by matched ID number
In this article, we will see how two data frames can be merged based on matched ID numbers. Approach Create a first data frameCreate a second data frameSelect Column to be matchedMerge using the merge function Syntax : DataFrame.merge(parameters) Display result Given below are implementations to produce a required result with the use of the require
4 min read
Merge two Pandas DataFrames with complex conditions
In this article, we let's discuss how to merge two Pandas Dataframe with some complex conditions. Dataframes in Pandas can be merged using pandas.merge() method. Syntax: pandas.merge(parameters) Returns : A DataFrame of the two merged objects. While working on datasets there may be a need to merge two data frames with some complex conditions, below
4 min read
Merge two Pandas DataFrames based on closest DateTime
In this article, we will discuss how to merge Pandas DataFrame based on the closest DateTime. To learn how to merge DataFrames first you have to learn that how to create a DataFrame for that you have to refer to the article Creating a Pandas DataFrame. After creating DataFrames need to merge them and to merge the Dataframe there's a function named
6 min read
How to Merge Two Pandas DataFrames on Index
In this article, we will discuss how to merge two Pandas Dataframes on Index in Python. Merge two Pandas DataFrames on Index using join() This join() method is used to join the Dataframe based on the index. C/C++ Code # import pandas module import pandas as pd # create student dataframe data1 = pd.DataFrame({'id': [1, 2, 3, 4], 'name': ['manoj', 'm
1 min read
Merge two Pandas DataFrames on certain columns
In data analysis, combining Pandas DataFrames is made easy with the merge function. You can streamline this process by pointing out which columns to use. Using a simple syntax, merging becomes a handy tool for efficiently working with data in various situations. This article walks you through the basic steps of merging Pandas DataFrames, providing
5 min read
How to Join Pandas DataFrames using Merge?
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. M
3 min read
Python | Merge, Join and Concatenate DataFrames using Pandas
A dataframe is a two-dimensional data structure having multiple rows and columns. In a Pandas DataFrame, the data is aligned in the form of rows and columns only. A dataframe can perform arithmetic as well as conditional operations. It has a mutable size. This article will show how to join, concatenate, and merge in Pandas. Python Merge, Join, and
4 min read
How to Merge DataFrames of different length in Pandas ?
In this article, we will discuss how to merge the two dataframes with different lengths in Pandas. It can be done using the merge() method. Syntax: DataFrame.merge(parameters) Below are some examples that depict how to merge data frames of different lengths using the above method: Example 1: Below is a program to merge two student data frames of di
2 min read