Merge two python pandas dataframes of different length but keep all rows in output dataframe

Given two pandas dataframes of different length, we have to merge them and keep all rows in output dataframe. By Pranit Sharma Last updated : October 03, 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

Suppose we are given two pandas DataFrames of different lengths containing some rows and columns that have some common values and some different values, we need to merge these two DataFrames so that the two corresponding columns from these two DataFrames have the same value so the rows of second DataFrame are appended to the corresponding row of first DataFrame.

Merging two python pandas dataframes of different length but keep all rows in output dataframe

For this purpose, we are going to use the merge() method which is going to be a left join. By default, merge performs an inner join operation. We will change this fashion by manipulating the arguments.

Pandas 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 python pandas dataframes of different length but keep all rows in output dataframe

# Importing pandas package
import pandas as pd

# Importing numpy package
import numpy as np

# Creating two dictionaries
d1 = {
    'a':[1,3,5,7,9],
    'b':['a','b','c','d','e'],
    'c':[0,0,0,0,0],
    'd':[0,0,0,0,0]
}

d2 = {
    'A':[3,5,1,7],
    'B':[2,4,6,8]
}

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

# Merging two dataframes whit left join
res = df1.merge(df2,how='left', left_on='a', right_on='A')

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

Output

The output of the above program is:

Example: Merge two python pandas dataframes of different length

Python Pandas Programs »

Comments and Discussions!

Load comments ↻





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