Product developers’ guide to customize data for AI — Part 2: Join & merge dataframes
Tags: machinelearning, programming, tutorial, python
Published: 2021-10-19 03:34:23

## TLDR
Before training ML models from multiple datasets, we need to understand how to relate datasets together. We’ll cover how to look at relationships in data using join and merge.

## Outline
- Introduction
- Before we begin
- Relationships
- Merge Dataframes
- Join Dataframes
- Conclusion

## Introduction
In this guide we’ll go over one of the most core concepts in AI that most search for today, how to combine dataframes with join, merge, or concat. Welcome to the 2nd part of the “Product developers’ guide to customizing data for AI”. In this article we’ll go over intermediate concepts and run through examples using merge and join in Pandas. We’ll look at the relationships between the email content dataset and user email dataset. By the end of this series, you’ll be ready to tackle designing datasets in order to begin training machine learning models.

## Before we begin
In this guide, we’ll reuse our [email_content dataset](https://app.box.com/s/tcloz4exgtckf4ed5yx1ykcv9tio3su0/file/850326590682) and include the [user_email dataset](https://app.box.com/s/tcloz4exgtckf4ed5yx1ykcv9tio3su0/file/850342238809). Like all other guides, we’ll be using Google Collab and start by importing those datasets into dataframes. There will be references back to [part 1](https://www.mage.ai/blog/product-developers-guide-to-customizing-data-for-ai-part-1) where we looked at folding, and basic set relations and continue off from there. If you aren’t familiar with transforming or sorting data, please read our [introductory guide](https://www.mage.ai/blog/product-developers-guide-to-getting-started-with-ai-pt-1-intro-dataframes) before continuing.

<center>![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zf2c9dt40urec3uoenhh.png)_Read from the start_</center>

## Relationships
Previously, we covered unions and intersections by concatenating dataframes. Building off of it, we introduce relative joins to always include a certain set.

### Relative joins
Another type of join is based on a certain side of the dataset. In set theory this is called a relative join, and contains everything on 1 side. In our case of having 2 datasets, we have 2 sides, the left side and the right side.

<center>![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zqcnnkrihsvl3i6lc2pj.png)_Include all values from the set (Source: [CodeProject](https://www.codeproject.com/Articles/33052/Visual-Representation-of-SQL-Joins))_</center>

## Merge dataframes
In Pandas, the merge function can be used together with keys, or unique pairs used to form the relationships seen in above. These are demonstrated best when there is data that is unique to both dataframes, in other words when the data doesn’t always exist.

Our starting dataframes are too ideal, so we’ll begin by spliting them to make it more realistic for an email campaign. We’ll take the leading 60 rows of the email_content, then ignore the 1st 10 email_id values from user_email. 

<center>![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4n69l2gh0kvtys4mt2j2.png)![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/guykzbhkuczbdcexn2ik.png)_This results in missing keys for email_id at the start, and id at the end_![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6azgutovena6pfvjgvvw.png)_The left contains values not in the right, and the right contains values not in the left (Source: [SO](https://stackoverflow.com/questions/53645882/pandas-merging-101))_</center>

We can use merge to establish the same relationships from our previous section, completing an inner and outer join as an intersection or union. Merge is quite useful when the columns’ names don’t match but we still want to relate them together. In this case our matching keys are the id in the campaign and email_id of the inbox.

### Intersection
To perform an intersection, we need 2 matching keys, and pass how=’inner’. To check our result by an additional parameter called indicator and set it to True.
<center>![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hj9xsdsof1ccvbrrilx7.png)_Areas where email_id and id have matching values 11 to 60._</center>
 
This is the correct result as the only valid _merge value for an intersection is both.

### Union
To perform a union, we repeat the process of matching keys, and pass how=‘outer’.
This time we’ll switch the order of the dataframes, choosing campaign on the left and inbox on the right. The results show the columns in the merge are displayed differently.

<center>![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u8dnqro8ci1ic1nuexa3.png)_Include all values in both, id and email_id ranging from 1 to 100_</center>

But merge can do more than concat, it can also perform the relative joins. This is categorized as a left or right join and may be used to relate the datasets. One such relation is columns, or features for ML models. Another relation is to introduce more rows, or samples for ML models.

### Left join
To start off, let’s do a left join on the user_inbox dataframe with the email_content.
Since email content and user inbox share the email_id we choose that as our parameter.
For our matching values, we have id from email_content, and on the left we have email_id from user_inbox. We’ll add back the indicator to check our result.

<center>![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eddjqreadn1vi454t64f.png)_From the campaign dataset there are values for ids ranging from 1 to 60_</center>

As a check, note that the _merge column doesn’t contain values for right_only.

### Right join
To create the right join in Pandas, we merge and pass the “on” values, using how=’right’. We include the indicator to check once again.

<center>![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ru3gccuapixxuuysl5b1.png)_From the inbox dataset, the email_id ranges from 11 to 100._</center>

A common misconception is that if we flip the order of the datasets, it will be the same as switching from left to right. This is wrong, because choosing which dataset is on the right and which dataset is on the left decides the column order as well.

Here we switch inbox and email, and perform another intersection.

<center>![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2yrutbor3i34nzravw8p.png)_The dimensions match the 1st example, but the order of the columns are changed_</center>

## Join dataframes
If you haven’t noticed, there’s the 2 pesky identical columns problem. In part 1, our previous approach was to convert the dataframes using concat and drop_duplicates, but here we don’t have a shared key. We can accomplish removing identical columns by applying the join and set_index method. This is a much better alternative, as its built-in functionality accomplishes it faster, looks cleaner, and with minimal effort or code.

### Set index
Before we can call join, we need to tell it what the similarities are. By default, join uses the index to determine which values to join on. As a result, the set index function is used similarly to merge’s “left_on=”, and “right_on=”. By changing the column into an index, it removes the column entirely. This is reminiscent of using drop_duplicates. But if you want the columns to remain you can include ‘drop=False’ to keep the columns.

### Join
Join works just like merge, instead of providing a list, the caller will represent the left, and the argument will represent the right, set_index is used to specify the match.

<center>![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rm72jqy72ahlo1nvluto.png)_Notice how the index columns for id and email_id are gone now_</center>

With drop=False, the result is similar to merge.

<center>![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/78drgc8wphvgvlejulk2.png)![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gieabc1nwb43n4cklhsj.png)_Result is similar, but the index values differ. Merge keeps row number, join was overwritten._</center>

## Conclusion
Merge handles everything join and concat does, but each excels at different situations. In most cases, merge is more versatile and therefore used more. In the next part, we’ll apply what we’ve learned to handle larger datasets, thus completing the entire marketing campaign featuring data on emails, the inbox, and user statistics.