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.
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 Guptaneed an explanation for this answer? contact us directly to get an explanation for this answer