Python Practice › Pandas Programs › Program 81

Merge on a common column

EasyPandas Programs · 81 of 95

Write a pandas program to join two DataFrames on their common column.

Advertisement
Output
   Id      Name  marks1  marks2
0  S2      Yash      87      70
1  S3  Abhishek      77      65

Approach

  1. merge(on="Id") keeps ids found in both.

Solution

Python
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
Advertisement

Learn the concept

New to this topic? Read the lesson, then come back.

More pandas programs

← All 95 Pandas Programs