Join two dataframes on common column

Given two pandas dataframes, we have to join them based on common column. By Pranit Sharma Last updated : September 18, 2023

Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and data.

Problem statement

We are given two DataFrames, both of these DataFrames have some common reference column, we need to join these DataFrame on this common column.

Joining two dataframes based on common column

We will use pandas.DataFrame.merge() method to join these two DataFrames.

Pandas merge() is a method of combining or joining two DataFrames but the key point is merge method allows us to combine the DataFrames based on specific columns instead of index values.

Inside this column, we will pass the required parameters like left_on values will be the first common column and right_on values would be the second common column.

Let us understand with the help of an example,

Python program to join two dataframes on common column

# Importing pandas package
import pandas as pd

# Importing numpy package
import numpy as np

# Creating two dictionaries
d1 = {
    'id':[1,2,3,4,5],
    'Name':['Ram','Shyam','Seeta','Geeta','Bablu'],
    'Age':[20,23,21,23,20]
}

d2 = {
    'id1':[1,2,3,4],
    'Salary':[20000,30000,25000,400000],
    'Department':['Peon','Junior Engg','Admin','Manager']
}

# Creating a DataFrames
df1 = pd.DataFrame(d1)
df2 = pd.DataFrame(d2)

# Display original DataFrames
print("Original Dataframe 1:\n",df1,"\n")
print("Original Dataframe 2:\n",df2,"\n")

# Joining two DataFrames
res = (pd.merge(df1, df2, left_on='id', right_on='id1', how='left').drop('id1', axis=1))

# Display Result
print('Result:\n',res)

Output

The output of the above program is:

Example: Join two dataframes on common column

Python Pandas Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.