Introduction to DataFrame and Pandas

Welcome aboard on our enlightening journey through merging DataFrames using pandas in Python! In the real world, data is rarely consolidated in one location. More often, it's spread across several sources, waiting to be collected, organized, and analyzed. Whether dealing with sales data from different regions, healthcare records from multiple facilities, or educational scores from several institutions, joining diverse chunks of data is a daily routine in any data-driven field.

In this lesson, we will learn how to use this powerful tool to combine DataFrames and discover the various merge operations and their usage in different scenarios. With practical examples to guide you, get ready to master the art of merging DataFrames with Python Pandas!

Basic Syntax for Merging DataFrames

We use the merge() function provided by pandas to combine DataFrames. This function combines two DataFrames and returns a captured DataFrame based on a common or shared column. Here's a general example:

In this example, abstract df1 and df2 are merged based on a shared or common column. The argument how="inner" denotes this as an inner merge.

Let's look at specific examples and unpack the four types of merges: inner join, outer join, left join, and right join.

Dataset

For this lesson, we will use the following dataset, stored in two separated dataframes:

Two important things to note:

  • The author with Author_ID=112 is missing in the df_authors dataframe
  • The book named Catcher in the df_books dataframe misses info about its author
Inner Join

An inner join includes rows where there is a match in both DataFrames. Here's how you can perform an inner join:

The resultant DataFrame will have only rows with common Author_ID in both dataframes, so we don't include books where author information is missing or undefined.

Outer Join

An outer join includes all the rows from both DataFrames and fills NaN for missing values:

All data from both dataframes is included. For any missing data, it has NaN. Note how it includes all the books, even if they miss the author info, and all the authors, even if there is no book info for them.

Left Join

A left join includes all rows from the first DataFrame and fills NaN for missing values in the second DataFrame:

After a left merge, the resultant DataFrame includes all books, even if they miss the author info.

Right Join

A right join includes all rows from the second DataFrame, in reverse to a left join. Here's an example of a right join:

After a right merge, the resultant DataFrame includes all authors, even if there is no book information for them.

Lesson Summary

Through this lesson, you have learned how to handle data-merging scenarios. This lesson has fortified your foundation for a critical aspect in data analysis and feature engineering: combining multiple datasets.

Get ready for more experiential learning in the following exercises, where you will apply the concepts you learned.

Without further ado, let's embark on our hands-on learning journey with interesting coding challenges—here's to happy coding!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal