ADVERTISEMENT
ADVERTISEMENT

How to merge multiple DataFrames on columns?

Given multiple DataFrames, we have to merge them on columns.
Submitted by Pranit Sharma, on May 19, 2022

DataFrames are 2-dimensional data structure in pandas. DataFrames consists of rows, columns and the data. DataFrame can be created with the help of python dictionaries or lists but in real world, csv files are imported and then converted into DataFrames.

We can have one, two or more than 2 DataFrames in pandas, and it may be possible that our required information is distributed in all of the DataFrames, the best way to deal with this situation is to join these DataFrames into a single DataFrame.

To merge two or more DataFrames into a single DataFrame, we use pandas.merge() method.

Syntax:

pandas.merge(
    left, 
    right, 
    how='inner', 
    on=None, 
    left_on=None, 
    right_on=None, 
    left_index=False, 
    right_index=False, 
    sort=False, 
    suffixes=('_x', '_y'), 
    copy=True, 
    indicator=False, 
    validate=None
    )

To work with pandas, we need to import pandas package first, below is the syntax:

import pandas as pd

Let us understand with the help of an example.

# Importing pandas package
import pandas as pd

# Creating a Dictionary
dict1 = {'Name':['Amit Sharma','Bhairav Pandey','Chirag Bharadwaj','Divyansh Chaturvedi','Esha Dubey']}
dict2 = {'Name':['Jatin prajapati','Rahul Shakya','Gaurav Dixit','Pooja Sharma','Mukesh Jha']}
dict3 = {'Name':['Ram Manohar','Sheetal Bhadoriya','Anand singh','Ritesh Arya','Aman Gupta']}

# Creating a DataFrame
df1 = pd.DataFrame(dict1)
df2 = pd.DataFrame(dict2)
df3 = pd.DataFrame(dict3)

# Display DataFrame
print("DataFrame1:\n",df1,"\n")
print("DataFrame2:\n",df2,"\n")
print("DataFrame3:\n",df3,"\n")

# Joining two DataFrames and then joining the first two DataFrames 
# with third DataFrame on the column Name
result = pd.merge(df1,df2,left_index=True, right_index=True)
result = pd.merge(result,df3,left_index=True, right_index=True)

# Display Result
pd.set_option('display.max_columns', 3)

print("Merged DataFrames:\n",result)

Output:

DataFrame1:
                   Name
0          Amit Sharma
1       Bhairav Pandey
2     Chirag Bharadwaj
3  Divyansh Chaturvedi
4           Esha Dubey 

DataFrame2:
               Name
0  Jatin prajapati
1     Rahul Shakya
2     Gaurav Dixit
3     Pooja Sharma
4       Mukesh Jha 

DataFrame3:
                 Name
0        Ram Manohar
1  Sheetal Bhadoriya
2        Anand singh
3        Ritesh Arya
4         Aman Gupta 

Merged DataFrames:
                 Name_x           Name_y               Name
0          Amit Sharma  Jatin prajapati        Ram Manohar
1       Bhairav Pandey     Rahul Shakya  Sheetal Bhadoriya
2     Chirag Bharadwaj     Gaurav Dixit        Anand singh
3  Divyansh Chaturvedi     Pooja Sharma        Ritesh Arya
4           Esha Dubey       Mukesh Jha         Aman Gupta

Python Pandas Programs »



ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.