values in the Subjects column are taken from both the 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) df5 = pandas.merge(df1, df2, how="right", on="Subjects") print("After right join: \n", df5)
Here also, the how parameter indicates we are performing one right join operation. And the merge operation is performed on the Subjects column as per the “on” parameter.
So, all the rows from df2 will be taken in df5. And only the rows that have common values in the Subjects column will be taken from both the DataFrames.
So, the output of the above program will be:
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 right join: Subjects Grades Marks_x Marks_y Remarks 0 Biology NaN NaN 85 Very Good 1 Chemistry C 72.0 72 Good 2 Literature NaN NaN 68 Fair
Merging DataFrames using an outer join
In the case of the outer join operation, all the rows from both the DataFrames will be taken in the output …






0 Comments