Python Practice › Pandas Programs › Program 82

Merge with different columns

EasyPandas Programs · 82 of 95

Write a pandas program to merge two DataFrames with different columns, keeping all rows.

Advertisement
Output
   Id  name   city
0   1  Asha    NaN
1   2  Ravi   Pune
2   3   NaN  Delhi

Approach

  1. how="outer".

Solution

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

More pandas programs

← All 95 Pandas Programs

Leave a Comment