- 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
- What is YII? and How to Install it?
- Understanding AI, ML, Data Science, and More: A Beginner's Guide to Choosing Your Career Path
- Understanding LLMs (Large Language Models): The Ultimate Guide for 2025
- 10 Awesome Data Science Blogs To Check Out
- Role of Digital Marketing Services to Uplift Online business of Company and Beat Its Competitors
- The Ultimate Guide to Machine Learning (ML) for Beginners
- Python Challenging Programming Exercises Part 1
- SQL Joins Explained: A Complete Guide with Examples
- Top 10 Knowledge for Machine Learning & Data Science Students
- Internet of Things (IoT) & AI – Smart Devices and AI Working Together
- Why to learn Digital Marketing?
- Best Platform to Learn Digital Marketing in Free
- Government Datasets from 50 Countries for Machine Learning Training
- Mastering SQL in 2025: A Complete Roadmap for Beginners
- Downlaod Youtube Video in Any Format Using Python Pytube Library
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
