We can use the merge() function to merge multiple DataFrames using the pandas Python library. The pandas merge operation is more like the join operation in SQL. Like SQL, we can perform inner join, left join, right join, and outer join operations on DataFrames using the merge() function. In this article, we will discuss each of these join operations using the merge() function by giving examples.
In this article, we will discuss:
- Merging DataFrames using an inner join
- Merging DataFrames using a left join
- Merging DataFrames using a right join
- Merging DataFrames using an outer join
Merging DataFrames using an inner join
Let’s first create two DataFrames.
import pandas dict1 = {"Subjects": ["Physics", "Chemistry", "Mathematics"], "Grades": ["A", "C", "B"], "Marks": [95, 72, 88]} df1 = pandas.DataFrame(dict1) print("df1: \n", df1) dict2 = {"Subjects": ["Biology", "Chemistry", "Literature"], "Marks": [85, 72, 68], "Remarks": ["Very Good", "Good", "Fair"]} df2 = pandas.DataFrame(dict2) print("df2: \n", df2)
The output is:






0 Comments