values in the Subjects column of both the DataFrames are taken from the right and the left DataFrames.
We can use the following Python code to merge DataFrames using the left join operation.
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) df4 = pandas.merge(df1, df2, how="left", on="Subjects") print("After left join: \n", df4)
Here also, we are using the how parameter to indicate we are performing one left join operation. And the “on” parameter indicates we are performing the merge operation on the “Subjects” column.
All the rows from df1 will be taken in df4. And only the row that has “Chemistry” in the Subjects column will be taken from the right and the left DataFrames. So, the output will be like the following:
df1: Subjects Grades Marks 0 Physics A 95 1 Chemistry C 72 2 Mathematics B 88 df2: Subjects Marks Remarks 0 Biology 85 Very Good 1 Chemistry 72 Good 2 Literature 68 Fair After left join: Subjects Grades Marks_x Marks_y Remarks 0 Physics A 95 NaN NaN 1 Chemistry C 72 72.0 Good 2 Mathematics B 88 NaN NaN
As we can see, values that are missing in either of the DataFrames, are indicated using NaN.
Merging DataFrames using a right join
In the case of the right join operation, all the rows from the right DataFrame are taken. And only the rows that have common …






0 Comments