Open In App

Merge two dataframes with same column names

Improve
Improve
Like Article
Like
Save
Share
Report

In this discussion, we will explore the process of Merging two dataframes with the same column names using Pandas. To achieve this, we’ll leverage the functionality of pandas.concat(), pandas.join(), and pandas.merge() functions. These methods handle the concatenation operations along a specified axis of Pandas objects, incorporating optional set logic (union or intersection) of the indexes on the other axes.

Merge two Dataframes with same column names

Here we are explaining some methods that depict how to Merge two dataframes with same column names, we are using some generally used methods for merging two dataframes with same column names which are the following :

Merge Two Dataframes using Pandas Concat() function

Syntax : pd.concat(objs, axis=0, join=’outer’, ignore_index=False, keys=None)

In this example, the below code uses Pandas to create two DataFrames, `data1` and `data2`, and displays them. It then merges the DataFrames along the rows (axis=0) using `pd.concat()`, but the result is not explicitly shown in the output.

Python3




# import module
import pandas as pd
 
# assign dataframes
data1 = pd.DataFrame([[1, 2, 3], [4, 5, 6], [7, 8, 9]],
                     columns=['A', 'B', 'C'])
 
data2 = pd.DataFrame([[3, 4], [5, 6]],
                     columns=['A', 'C'])
 
# display dataframes
print('Dataframes:')
display(data1)
display(data2)
 
# merge two data frames
print('After merging:')
pd.concat([data1, data2], axis=0)


Output:

Dataframes:
   A  B  C
0  1  2  3
1  4  5  6
2  7  8  9
   A  C
0  3  4
1  5  6
After merging:
   A  B  C
0  1  2  3
1  4  5  6
2  7  8  9
0  3  4  0
1  5  6  1

Merge Two Dataframe using Pandas Join() Function

Syntax : pd.merge(left, right, how=’inner’, on=None, left_on=None, right_on=None)

In this example the code creates and displays two Pandas DataFrames, `data1` and `data2`. It then merges them along the ‘Students’ column using the `join` method with an outer join, distinguishing column names with suffixes. The merged result is not explicitly shown in the output.

Python3




# import module
import pandas as pd
 
# assign dataframes
data1 = pd.DataFrame([[25, 77.5, 'A'], [30, 60.2, 'B']],
                     columns=['Students', 'Avg Marks', 'Section'])
 
data2 = pd.DataFrame([[52, 'C'], [25, 'A']],
                     columns=['Students', 'Section'])
 
# display dataframes
print('Dataframes:')
display(data1)
display(data2)
 
# merge two data frames using join
print('After merging:')
data1.set_index('Students', inplace=True)
data2.set_index('Students', inplace=True)
data1.join(data2, lsuffix='_left', rsuffix='_right', how='outer').reset_index()


Output:

Dataframes:
   Students  Avg Marks Section
0        25      77.5       A
1        30      60.2       B
   Students Section
0        52       C
1        25       A
After merging:
   Students  Avg Marks Section_left Section_right
0        25      77.5            A             A
1        30      60.2            B           NaN
2        52       NaN          NaN             C

Merge Two Dataframe using Pandas merge() function

Syntax : DataFrame.join(other, on=None, how=’left’, lsuffix=”, rsuffix=”, sort=False)

In this example below code creates and displays two Pandas DataFrames, data1 and data2, representing student information. It then merges the DataFrames using an outer join based on the common columns (‘Students,’ ‘Avg Marks,’ ‘Section’). The result is displayed, combining information from both DataFrames.

Python3




# import module
import pandas as pd
 
# assign dataframes
data1 = pd.DataFrame([[25, 77.5, 'A'], [30, 60.2, 'B'],
                      [25, 70.7, 'C']],
                     columns=['Students', 'Avg Marks', 'Section'])
 
data2 = pd.DataFrame([[30, 70.2, 'A'], [25, 65.2, 'B'],
                      [35, 77.7, 'C']],
                     columns=['Students', 'Avg Marks', 'Section'])
 
# display dataframes
print('Dataframes:')
display(data1)
display(data2)
 
# merge two data frames
print('After merging:')
pd.concat([data1, data2], axis=0)


Output:

Dataframes:
   Students  Avg Marks Section
0        25      77.5       A
1        30      60.2       B
2        25      70.7       C
   Students  Avg Marks Section
0        30      70.2       A
1        25      65.2       B
2        35      77.7       C
After merging:
   Students  Avg Marks Section
0        25      77.5       A
1        30      60.2       B
2        25      70.7       C
0        30      70.2       A
1        25      65.2       B
2        35      77.7       C


Last Updated : 04 Dec, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads