How to concat two dataframes with different column names in pandas?

Given two pandas dataframes with different column names, we have to concat them.
Submitted by Pranit Sharma, on November 26, 2022

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 Pandas data frames and these two Pandas dataframes have the same name columns but we need to merge the two data frames using the keys of the first data frame and we need to tell the Pandas to place the values of another column of the second data frame in the first column of the first data frame.

Contacting two dataframes with different column names

For this purpose, we will use the concat() method, and also inside this concat() method we will use the rename() method with the second data frame where we will rename the second column of the second data frame with the first column of the second data frame so that the concat method understands that the values of the second column need to get merged with the values of the first column of the first dataframe.

Let us understand with the help of an example,

Python code to concat two dataframes with different column names in pandas

# Importing pandas package
import pandas as pd

# Importing numpy package
import numpy as np

# Creating dictionaries

d1 = {'a':[10,20,30],'x':[40,50,60],'y':[70,80,90]}
d2 = {'b':[10,11,12],'x':[13,14,15],'y':[16,17,18]}

# 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 dfs and renaming columns of second df
res = pd.concat([df1,df2.rename(columns={'b':'a'})], ignore_index=True)

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

Output

The output of the above program is:

Example: Concat two dataframes with different column names

Python Pandas Programs »

Comments and Discussions!

Load comments ↻





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