Python Practice › Pandas Programs › Program 82
Merge with different columns
Write a pandas program to merge two DataFrames with different columns, keeping all rows.
Output
Id name city 0 1 Asha NaN 1 2 Ravi Pune 2 3 NaN Delhi
Approach
how="outer".
Solution
import pandas as pdd1 = pd.DataFrame({"Id": [1, 2], "name": ["Asha", "Ravi"]})d2 = pd.DataFrame({"Id": [2, 3], "city": ["Pune", "Delhi"]})print(d1.merge(d2, on="Id", how="outer"))Output
Id name city 0 1 Asha NaN 1 2 Ravi Pune 2 3 NaN Delhi
Learn the concept
New to this topic? Read the lesson, then come back.