Hello! Welcome back to our module on Data Analysis and Visualization.
In our previous lesson, we built a strong foundation in data cleaning by tackling missing values, duplicates, and outliers. Now that you have the skills to create clean, reliable datasets, it's time to learn how to combine them. Real-world AI projects often require you to pull information from multiple sources—user profiles, transaction logs, product details, sensor readings—and integrate it into a single, comprehensive dataset.
This lesson directly addresses the learning outcome: Merge, join, and concatenate multiple DataFrames. We will explore the three primary pandas functions for this purpose: merge(), concat(), and .join(). Mastering these operations is a fundamental skill for preparing data for analysis, visualization, and, most importantly, for training machine learning models.
A Quick Overview
Before we dive into the details, let's get a high-level picture of the three functions we'll be covering.

Now, let's explore each of these in detail.
1. pd.merge(): The Go-To for Database-Style Joins
The pd.merge() function is the most powerful and flexible tool for combining DataFrames. It's designed to replicate the logic of SQL JOIN operations, combining rows from two DataFrames based on the values in one or more common columns, known as "keys".
There are four main types of joins you can perform with merge(), controlled by the how parameter.

To see how these work in practice, we'll watch a video that walks through creating and merging DataFrames.
Merging DataFrames in Pandas | Python Pandas Tutorials
The video 'Merging DataFrames in Pandas' by Alex The Analyst provides a clear, step-by-step guide to using pd.merge(). We'll go through it section by section.
First, watch the setup and the explanation of the default inner join (00:00 - 05:20). Introduction (00:00 - 00:56): The video starts with a conceptual overview of inner, outer, left, and right joins, similar to the image above. Setup (00:56 - 02:12): See how two simple DataFrames are created for the examples. Inner Join (03:14 - 05:20): Pay close attention to how the inner join works by default. It finds rows where the key column(s) have matching values in both DataFrames and returns only those combined rows.
As you saw, the on parameter is crucial for telling pandas which column(s) to use as the key(s) for the merge. If you don't specify on, pandas will use all columns that have the same name in both DataFrames. It's best practice to always be explicit and use the on parameter.
One-to-Many and Suffixes
What happens if a key appears multiple times in one DataFrame, or if the non-key columns have the same name? The next video segment addresses this.
Merging DataFrames in Pandas | Python Pandas Tutorials
Let's continue with the same video to see how to specify join keys and handle overlapping column names.
Watch from 05:20 to 07:50. This part demonstrates: Using the on parameter to specify a single join key ('FellowshipID'). How pandas automatically adds suffixes (_x, _y) to columns that have the same name in both DataFrames but are not part of the join key.
You can customize these suffixes using the suffixes=('_left', '_right') argument in merge(), which is useful for clarity.
Outer, Left, and Right Joins
Now let's explore the other join types, which allow you to keep data that doesn't have a match.
- Outer Join (
how='outer'): Keeps all rows from both DataFrames. If a row from one DataFrame doesn't have a match in the other, the columns from the other DataFrame will be filled withNaN(Not a Number). - Left Join (
how='left'): Keeps all rows from the left DataFrame. It only includes data from the right DataFrame where the key matches. Unmatched rows will haveNaNfor the right DataFrame's columns. - Right Join (
how='right'): The opposite of a left join. It keeps all rows from the right DataFrame.
Merging DataFrames in Pandas | Python Pandas Tutorials
Let's watch the final segments on merge to see these other join types in action.
Watch from 07:50 to 11:18. The video will cover: Outer Join (07:50 - 09:17): Notice how all rows from both original DataFrames are present in the result. Left Join (09:17 - 10:13): See how the result has all the rows from the first (left) DataFrame. Right Join (10:13 - 11:18): Observe how this is the mirror image of the left join, keeping all rows from the second (right) DataFrame.
2. pd.concat(): Stacking DataFrames
While merge() is for combining DataFrames based on shared values, concat() is for stacking them on top of each other (concatenating rows) or side-by-side (concatenating columns).
The primary parameter for concat() is axis:
axis=0(default): Stacks DataFrames vertically. Pandas will align data based on column names.axis=1: Stacks DataFrames horizontally. Pandas will align data based on the index.
Merging DataFrames in Pandas | Python Pandas Tutorials
Let's return to the video to see how concat works for both vertical and horizontal stacking.
Watch from 17:54 to 21:27. Default concat (17:54 - 19:40): See how pd.concat([df1, df2]) simply appends the rows of df2 to the end of df1. The axis parameter (19:40 - 20:29): Understand how axis=1 changes the behavior to place the DataFrames side-by-side. append is Deprecated (20:29 - 21:27): The video also mentions the older .append() method and correctly states that you should always use pd.concat() instead.
A useful argument for concat() is ignore_index=True, which creates a new, continuous index for the resulting DataFrame (0, 1, 2, ...), which is usually what you want when stacking rows.
3. DataFrame.join(): A Shortcut for Index-Based Joins
The DataFrame.join() method is a convenient alternative to merge() specifically for combining DataFrames based on their index. By default, it performs a left join using the calling DataFrame's index.
While you can make .join() work on columns, it often requires you to first set the desired column as the index using .set_index(). For this reason, pd.merge() is generally more direct and flexible for column-based joins.
Let's see the distinction.
Merging DataFrames in Pandas | Python Pandas Tutorials
The final video segment demonstrates how .join() works and highlights its index-centric nature.
Watch from 12:47 to 17:54. The video first shows the difficulty of using .join() on columns directly, then demonstrates the correct, index-based approach: Initial Attempt (12:47 - 15:01): Notice the errors and complications when trying to use .join() like merge(). The Index-Based Solution (15:01 - 17:54): The key insight here is using .set_index() on the DataFrames before calling .join(). This makes the operation clean and effective.
When to Use Which? A Quick Guide
pd.merge(): Your default choice for combining data. Use it when you need to join DataFrames based on common columns, just like in a database. It's the most explicit and powerful option.pd.concat(): Use this when you need to append rows from one DataFrame to another, or to place two DataFrames with the same index side-by-side. Think "stacking".df.join(): A convenient shortcut for when you specifically need to perform a join on the DataFrames' indices.
Conclusion
Great job! You have now learned the essential techniques for combining data from multiple sources into a single, unified DataFrame. This is a critical and frequently performed step in any data analysis or machine learning workflow.
Key Takeaways:
pd.merge()is your most versatile tool for database-style joins on common columns (keys). You can control the logic withhow='inner','outer','left', or'right'.pd.concat()is used for stacking DataFrames. Useaxis=0to append rows andaxis=1to place them side-by-side.df.join()is a specialized method for convenience when joining DataFrames on their indices.
In our next lesson, we will build directly on this. Now that we can create a single, comprehensive dataset, we'll learn how to summarize it. We'll cover how to perform groupby operations and create pivot tables for data aggregation, allowing you to calculate summary statistics for different segments of your data and uncover powerful insights.