In this article, we will see how two data frames can be merged based on matched ID numbers.
Approach
- Create a first data frame
- Create a second data frame
- Select Column to be matched
- Merge using the merge function
Syntax : DataFrame.merge(parameters)
Given below are implementations to produce a required result with the use of the required parameter with an appropriate value.
Example:
Python3
import pandas as pd
df1 = pd.DataFrame({ 'ID' : [ 1 , 2 , 3 , 5 , 7 , 8 ],
'Name' : [ 'Sam' , 'John' , 'Bridge' ,
'Edge' , 'Joe' , 'Hope' ]})
df2 = pd.DataFrame({ 'ID' : [ 1 , 2 , 4 , 5 , 6 , 8 , 9 ],
'Marks' : [ 67 , 92 , 75 , 83 , 69 , 56 , 81 ]})
df = pd.merge(df1, df2, on = "ID" )
print (df)
|
Output :

Merged Dataframe
Merging two Dataframes with the ID column, with all the ID’s of the left Dataframe i.e. first parameter of the merge function. The ID’s which are not present in df2 gets a NaN value for the columns of that row.
Example 2 :
Python3
import pandas as pd
df1 = pd.DataFrame({ 'ID' : [ 1 , 2 , 3 , 5 , 7 , 8 ],
'Name' : [ 'Sam' , 'John' , 'Bridge' ,
'Edge' , 'Joe' , 'Hope' ]})
df2 = pd.DataFrame({ 'ID' : [ 1 , 2 , 4 , 5 , 6 , 8 , 9 ],
'Marks' : [ 67 , 92 , 75 , 83 , 69 , 56 , 81 ]})
df = pd.merge(df1, df2, on = "ID" , how = "left" )
print (df)
|
Output :

Merged Dataframe
Merging two Dataframes with the ID column, with all the ID’s of the right Dataframe i.e. second parameter of the merge function. The ID’s which do not match from df1 gets a NaN value for that column.
Example 3 :
Python3
import pandas as pd
df1 = pd.DataFrame({ 'ID' : [ 1 , 2 , 3 , 5 , 7 , 8 ],
'Name' : [ 'Sam' , 'John' , 'Bridge' ,
'Edge' , 'Joe' , 'Hope' ]})
df2 = pd.DataFrame({ 'ID' : [ 1 , 2 , 4 , 5 , 6 , 8 , 9 ],
'Marks' : [ 67 , 92 , 75 , 83 , 69 , 56 , 81 ]})
df = pd.merge(df1, df2, on = "ID" , how = "right" )
print (df)
|
Output :

Merged Dataframe
Merging two Dataframes with the ID column, with all that match in both the dataframes.
Example 4 :
Python3
import pandas as pd
df1 = pd.DataFrame({ 'ID' : [ 1 , 2 , 3 , 5 , 7 , 8 ],
'Name' : [ 'Sam' , 'John' , 'Bridge' ,
'Edge' , 'Joe' , 'Hope' ]})
df2 = pd.DataFrame({ 'ID' : [ 1 , 2 , 4 , 5 , 6 , 8 , 9 ],
'Marks' : [ 67 , 92 , 75 , 83 , 69 , 56 , 81 ]})
df = pd.merge(df1, df2, on = "ID" , how = "inner" )
print (df)
|
Output :

Merged Dataframe
Merging two Dataframes with the ID column, with all the ID’s of both the dataframes and NaN value for the columns where the ID is not found in both the dataframes.
Example 5 :
Python3
import pandas as pd
df1 = pd.DataFrame({ 'ID' : [ 1 , 2 , 3 , 5 , 7 , 8 ],
'Name' : [ 'Sam' , 'John' , 'Bridge' ,
'Edge' , 'Joe' , 'Hope' ]})
df2 = pd.DataFrame({ 'ID' : [ 1 , 2 , 4 , 5 , 6 , 8 , 9 ],
'Marks' : [ 67 , 92 , 75 , 83 , 69 , 56 , 81 ]})
df = pd.merge(df1, df2, on = "ID" , how = "outer" )
print (df)
|
Output :

Merged DataFrame
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
05 Apr, 2021
Like Article
Save Article