Merge two dataframes based on multiple keys in pandas

Given two pandas dataframes, we have to merge them based on multiple keys in pandas. By Pranit Sharma Last updated : September 19, 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 a and b and we need to merge these DataFrames on the basis of the columns of these DataFrames.

Merging two pandas dataframes based on multiple keys

We will use the pd.merge() method of pandas DataFrames for this purpose. Pandas pd.merge() is a method of combining or joining two DataFrames but the key point is merge method allows us to combine the DataFrames on the basis of specific columns instead of index values.

Let us understand with the help of an example,

Python program to merge two dataframes based on multiple keys in pandas

# Importing pandas package
import pandas as pd

# Creating a dictionary
d1 = {
    'A': [1,1,2,2],
    'B': [1,2,1,2],
    'Key1':[23,34,233,43]
}

d2 = {
    'A': [1,2,3,4],
    'B': [1,2,3,4],
    'Key2':[0.1,0.2,0.13,0.333]
}

# Creating two 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")

# Combining two dataframes
res = pd.merge(df1,df2, on=['A', 'B'])

# Display result
print("Result:\n",res)

Output

The output of the above program is:

Example: Merge two dataframes based on multiple keys

Python Pandas Programs »


Comments and Discussions!

Load comments ↻






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