# Write a program in Python to merge two dataframes based on matching data in a column

PythonPandasServer Side ProgrammingProgramming

#### Beyond Basic Programming - Intermediate Python

Most Popular

36 Lectures 3 hours

#### Practical Machine Learning using Python

Best Seller

91 Lectures 23.5 hours

#### Practical Data Science using Python

22 Lectures 6 hours

Assume, you have two dataframe,

first dataframe is
id country
0 1 India
1 2 UK
2 3 US
3 4 China

second dataframe is

id City
0 1 Chennai
1 11 Cambridge
2 22 Chicago
3 4 Chengdu

And the result for merging based on same column is,

Merging data based on same column - id
id    country    City
0 1    India    Chennai
1 4    China    Chengdu

## Solution

To solve this, we will follow the steps given below −

• Define a two dataframes

• Merge two dataframes based on the same column id is defined below,

pd.merge(first_df,second_df,how='inner',on='id')

### Example

Let us see the following implementation to get a better understanding −

import pandas as pd
import numpy as np
first_df = pd.DataFrame({'id':[1,2,3,4], 'country':['India','UK','US','China']
})
print("first dataframe is", first_df)
second_df = pd.DataFrame({'id':[1,11,22,4], 'City' : ['Chennai','Cambridge','Chicago','Chengdu']
})
print("second dataframe is", second_df)
print("Merging data based on same column - id")
print(pd.merge(first_df,second_df,how='inner',on='id'))

### Output

first dataframe is
id country
0 1 India
1 2 UK
2 3 US
3 4 China

second dataframe is
id  City
0 1 Chennai
1 11 Cambridge
2 22 Chicago
3 4 Chengdu

Merging data based on same column - id
id country City
0 1 India Chennai
1 4 China Chengdu
Updated on 24-Feb-2021 09:41:12