Instagram
youtube
Facebook
Twitter

Merge Two Pandas DataFrames on a Common Column

A Merge Two Pandas DataFrames on a Common Column?
Code Explanation:
● DataFrame Creation: Two DataFrames with a common 'ID' column are created.
● Merge: pd.merge() is used to combine the DataFrames on 'ID'.
● Store Result: Merged result is saved in merged_df.
● Output Display: Final merged DataFrame is printed.

 

Program:

import pandas as pd

df1 = pd.DataFrame({'ID': [1, 2], 'Name': ['Alice', 'Bob']})

df2 = pd.DataFrame({'ID': [1, 2], 'Score': [90, 95]})

merged_df = pd.merge(df1, df2, on='ID')

print(merged_df)