- 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
When 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
- Debugging in Python
- Multithreading and Multiprocessing in Python
- Context Managers in Python
- Decorators in Python
- Generators in Python
- Requests in Python
- Django
- Flask
- Matplotlib/Seaborn
- Pandas
- NumPy
- Modules and Packages in Python
- File Handling in Python
- Error Handling and Exceptions in Python
- Indexing and Performance Optimization in SQL
Random Blogs
- Loan Default Prediction Project Using Machine Learning
- Understanding HTAP Databases: Bridging Transactions and Analytics
- Ideas for Content of Every niche on Reader’s Demand during COVID-19
- Downlaod Youtube Video in Any Format Using Python Pytube Library
- Grow your business with Facebook Marketing
- SQL Joins Explained: A Complete Guide with Examples
- The Ultimate Guide to Machine Learning (ML) for Beginners
- Datasets for Exploratory Data Analysis for Beginners
- Datasets for analyze in Tableau
- 5 Ways Use Jupyter Notebook Online Free of Cost
- AI & Space Exploration – AI’s Role in Deep Space Missions and Planetary Research
- AI in Cybersecurity: The Future of Digital Protection
- Understanding AI, ML, Data Science, and More: A Beginner's Guide to Choosing Your Career Path
- Top 10 Knowledge for Machine Learning & Data Science Students
- Quantum AI – The Future of AI Powered by Quantum Computing
Datasets for Machine Learning
- 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
- Bitcoin Heist Ransomware Address Dataset