- Data Analysis with Python
-
Overview
- Introduction to Data Science and Analytics
- Loading and Cleaning Data in Pandas
- Data Manipulation with NumPy and Pandas
- Exploratory Data Analysis (EDA) Techniques
- Handling Missing Data and Duplicates
- Merging, Joining, and Concatenating DataFrames
- Time Series Analysis Basics
- Data Visualization with Matplotlib and Seaborn
- Descriptive Statistics and Data Summarization
- Advanced Pandas Operations
Merging, Joining, and Concatenating DataFrames
Add to BookmarkWhen working with large datasets in Python, we often need to combine multiple DataFrames. Pandas provides powerful functions for merging, joining, and concatenating datasets efficiently. In this tutorial, we will explore these techniques with practical examples.
1. Concatenating DataFrames
Concatenation in Pandas is used to stack DataFrames either vertically (along rows) or horizontally (along columns).
Concatenating Along Rows (axis=0)
import pandas as pd
data1 = pd.DataFrame({'ID': [1, 2], 'Name': ['Amit', 'Priya']})
data2 = pd.DataFrame({'ID': [3, 4], 'Name': ['Rahul', 'Sneha']})
result = pd.concat([data1, data2], ignore_index=True)
print(result)
Concatenating Along Columns (axis=1)
data3 = pd.DataFrame({'Age': [25, 30]})
merged_data = pd.concat([data1, data3], axis=1)
print(merged_data)
2. Merging DataFrames
Merging is similar to SQL joins and allows combining DataFrames based on common columns.
Inner Join (Default Merge)
dataA = pd.DataFrame({'ID': [1, 2, 3], 'Name': ['Amit', 'Priya', 'Rahul']})
dataB = pd.DataFrame({'ID': [2, 3, 4], 'Salary': [50000, 60000, 70000]})
merged_data = pd.merge(dataA, dataB, on='ID')
print(merged_data)
Left Join
left_join = pd.merge(dataA, dataB, on='ID', how='left')
print(left_join)
Right Join
right_join = pd.merge(dataA, dataB, on='ID', how='right')
print(right_join)
Outer Join (Full Join)
outer_join = pd.merge(dataA, dataB, on='ID', how='outer')
print(outer_join)
3. Joining DataFrames
Pandas join()
is used when combining DataFrames based on index.
df1 = pd.DataFrame({'Salary': [50000, 60000]}, index=['Amit', 'Priya'])
df2 = pd.DataFrame({'Age': [25, 30]}, index=['Amit', 'Priya'])
joined_df = df1.join(df2)
print(joined_df)
Summary
- Concatenation: Used for stacking DataFrames vertically or horizontally.
- Merging: Similar to SQL joins, merging combines DataFrames based on a key column.
- Joining: Works on index-based merging.
Prepare for Interview
- JavaScript Interview Questions for 5+ Years Experience
- JavaScript Interview Questions for 2–5 Years Experience
- JavaScript Interview Questions for 1–2 Years Experience
- JavaScript Interview Questions for 0–1 Year Experience
- JavaScript Interview Questions For Fresher
- SQL Interview Questions for 5+ Years Experience
- SQL Interview Questions for 2–5 Years Experience
- SQL Interview Questions for 1–2 Years Experience
- SQL Interview Questions for 0–1 Year Experience
- SQL Interview Questions for Freshers
- Design Patterns in Python
- Dynamic Programming and Recursion in Python
- Trees and Graphs in Python
- Linked Lists, Stacks, and Queues in Python
- Sorting and Searching in Python
Random Blogs
- Mastering SQL in 2025: A Complete Roadmap for Beginners
- Top 10 Knowledge for Machine Learning & Data Science Students
- Quantum AI – The Future of AI Powered by Quantum Computing
- Navigating AI Careers in 2025: Data Science, Machine Learning, Deep Learning, and More
- What Is SEO and Why Is It Important?
- Generative AI - The Future of Artificial Intelligence
- String Operations in Python
- Convert RBG Image to Gray Scale Image Using CV2
- Understanding Data Lake, Data Warehouse, Data Mart, and Data Lakehouse – And Why We Need Them
- How AI Companies Are Making Humans Fools and Exploiting Their Data
- How to Start Your Career as a DevOps Engineer
- AI & Space Exploration – AI’s Role in Deep Space Missions and Planetary Research
- How to Become a Good Data Scientist ?
- The Ultimate Guide to Artificial Intelligence (AI) for Beginners
- The Ultimate Guide to Machine Learning (ML) for Beginners
Datasets for Machine Learning
- Awesome-ChatGPT-Prompts
- Amazon Product Reviews Dataset
- Ozone Level Detection Dataset
- Bank Transaction Fraud Detection
- YouTube Trending Video Dataset (updated daily)
- Covid-19 Case Surveillance Public Use Dataset
- US Election 2020
- Forest Fires Dataset
- Mobile Robots Dataset
- Safety Helmet Detection
- All Space Missions from 1957
- OSIC Pulmonary Fibrosis Progression Dataset
- Wine Quality Dataset
- Google Audio Dataset
- Iris flower dataset