- 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
- 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
- Debugging in Python
- Unit Testing in Python
- Asynchronous Programming in PYthon
- Multithreading and Multiprocessing in Python
- Context Managers in Python
- Decorators in Python
Random Blogs
- AI in Marketing & Advertising: The Future of AI-Driven Strategies
- Datasets for Exploratory Data Analysis for Beginners
- Python Challenging Programming Exercises Part 2
- The Ultimate Guide to Artificial Intelligence (AI) for Beginners
- Google’s Core Update in May 2020: What You Need to Know
- The Ultimate Guide to Data Science: Everything You Need to Know
- Understanding OLTP vs OLAP Databases: How SQL Handles Query Optimization
- Understanding Data Lake, Data Warehouse, Data Mart, and Data Lakehouse – And Why We Need Them
- Types of Numbers in Python
- Mastering Python in 2025: A Complete Roadmap for Beginners
- Python Challenging Programming Exercises Part 3
- How to Become a Good Data Scientist ?
- How AI Companies Are Making Humans Fools and Exploiting Their Data
- Store Data Into CSV File Using Python Tkinter GUI Library
- Convert RBG Image to Gray Scale Image Using CV2
Datasets for Machine Learning
- 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
- Artificial Characters Dataset