Mastering DataFrame Merging in Python with Pandas

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:

merged_df = df1.merge(df2, on="common_column", how="inner")

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:

df_books = pd.DataFrame({
     "Book_ID": [1, 2, 3, 4, 5],
     "Book_Title": ['Gatsby', 'Mockingbird', '1984', 'Catcher', 'LOTR'],
     "Author_ID": [101, 102, 103, None, 112],
     "Genre": ['Fiction', 'Fiction', 'Fiction', 'Fiction', 'Fantasy']
 })

# creating the DataFrame for Authors
df_authors = pd.DataFrame({
    "Author_ID": [101, 102, 103, 104, 105],
    "Author_Name": ['F. Fitzgerald', 'H. Lee', 'G. Orwell', 'J. Salinger', 'J. Tolkien'],
    "Nationality": ['American', 'American', 'British', 'American', 'British']
})

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:

# Merge the dataframes - inner merge
merged_df = df_books.merge(df_authors, on="Author_ID", how="inner")
print(merged_df)
'''Output:
   Book_ID   Book_Title  Author_ID    Genre    Author_Name Nationality
0        1       Gatsby      101.0  Fiction  F. Fitzgerald    American
1        2  Mockingbird      102.0  Fiction         H. Lee    American
2        3         1984      103.0  Fiction      G. Orwell     British
'''

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.

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