Python Practice › Pandas Programs › Program 81
Merge on a common column
Write a pandas program to join two DataFrames on their common column.
Output
Id Name marks1 marks2 0 S2 Yash 87 70 1 S3 Abhishek 77 65
Approach
merge(on="Id")keeps ids found in both.
Solution
import pandas as pdd1 = pd.DataFrame({"Id": ["S1", "S2", "S3"], "Name": ["Ketan", "Yash", "Abhishek"], "marks1": [90, 87, 77]})d2 = pd.DataFrame({"Id": ["S2", "S3", "S4"], "marks2": [70, 65, 88]})print(d1.merge(d2, on="Id"))Output
Id Name marks1 marks2 0 S2 Yash 87 70 1 S3 Abhishek 77 65
Learn the concept
New to this topic? Read the lesson, then come back.