How to combine two data Frame in Python – Pandas

This article will teach you how to combine two data frames using a Python program with a simple, understandable code.

In the Python programming languages, we are using the Pandas library, which is a popular open-source Python library to combine two data frames. By using it, we can load, clean, analyze, and visualize the data

Combining two data frames in Python

Firstly, we will discuss merging two data frames using the merge method. Combining Data Frames in a common field is called joining. Displaying the values in the columns that contain the common values is called joining the Data Frame

Merging Data Frames in Pandas

Here, we are creating two data frames that contain two columns named age and name. In the merge method, there are three types of joins involved.

  • Right Join
  • Inner Join
  • Left Join

Note: The process of joining methods is similar to the SQL database.

Example 1: Right Join

The left column displays the common values between right and left columns.

import pandas as pd
df1=pd.DataFrame({'name':['suresh','john','sarathi','ramesh','joseph'],'age':[32,36,33,34,31]})
df2=pd.DataFrame({'name':['sathish','john','dinesh','ramesh','Suresh'],'age':[37,36,29,34,32]})
result=df1.merge(df2,how='right',on='age')
Output:
  name_x age name_y
0    NaN 37  sathish
1   john 36     john
2    NaN 29   dinesh
3 ramesh 34   ramesh
4 suresh 32   suresh

Example 2: Inner Join

In the inner join method, only the common values are displayed in the data frame

import pandas as pd
df1=pd.DataFrame({'name':['suresh','john','sarathi','ramesh','joseph'],'age':[32,36,33,34,31]})
df2=pd.DataFame({'name':['sathish','john','dinesh','ramesh','suresh'],'age':[37,36,29,34,32]})
result=df1.merge(df2,how='inner',left_on='age',right_on='age')
Output:
  name_x age name_y
0 suresh 32  suresh
1   john 36    john
2 ramesh 34  ramesh

Example 3: Left Join

The right column displays the common values between right and left columns.

import pandas as pd
df1=pd.DataFrame({'name':['suresh','john','sarathi','ramesh','joseph'],'age':[32,36,33,34,31]})
df2=pd.DataFrame({'name':['sathish','john','dinesh','ramesh','suresh'],'age':[37,36,29,34,32]})
result=df1.merge(df2,how='left',on='age')
Output:
  name_x  age name_y
0 suresh  32  suresh   
1   john  36    john
2 sarathi 33     NaN
3  ramesh 34  ramesh
4  joseph 31     NaN  

Concatenating Data Frames using Pandas

The concatenate method is a bit different from the merge method, which we have discussed above. To combine two data frames into single, we can use this concatenating function.

Here, we are creating two data frames that contain two columns named Employee id and Place.

Example 1: Concatenating two data frames into single

import pandas as pd
df1=pd.DataFrame({'Employee id':['A12',A13,'A14','A15'],'Place':[23,27,32,35]})
df2=pd.DataFrame(['Employee id':['B21','B22','B23','B24'],'Place':[32,38,28,27]})
dataframes=[df1,df2]
result=pd.concat(dataframes).reset_index(drop=True)
Output:
  Employee id Place
0         A12    23
1         A13    27
2         A14    32
3         A15    35
4         B21    32
5         B22    38
6         B23    28
7         B24    27

Example 2: If you want to Concat horizontally

import pandas as pd
df1=pd.DataFrame({'Employee id':['A12','A13','A14','A15'],'Place':[23,27,32,35]})
df2=pd.DataFrame({'Employee id':['B21','B22','B23','B24'],'Place':[26,33,28,27]})
dataframes=[df1,df2]
result=pd.concat([df1,df2],axis=1)
Output:
  Employee id Place Employee id Place
0         A12   23          B21    26
1         A13   27          B22    33
2         A14   32          B23    28
3         A15   35          B24    27

Conclusion

In this article, you have learned the process of creating a simple python program to combine two data frames in pandas, this is the basic python program you can modify with your specifications. Thank you for learning with us. Have a great journey.

 

 

 

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top