Doing one-to-one merges by multiple columns
The same logic we used to perform one-to-one merges with one merge-by column applies to merges we perform with multiple merge-by columns. Inner, outer, left, and right joins work the same way when you have two or more merge-by columns. We will demonstrate this in this recipe.
Getting ready
We will work with the NLS data in this recipe, specifically weeks worked and college enrollment from 2017 through 2021. Both the weeks worked and college enrollment files contain one row per person, per year.
How to do it...
We will do a one-to-one merge with two DataFrames using multiple merge-by columns on each DataFrame. Let’s get started:
- Import
pandas
and load the NLS weeks worked and college enrollment data:import pandas as pd nls97weeksworked = pd.read_csv("data/nls97weeksworked.csv") nls97colenr = pd.read_csv("data/nls97colenr.csv")
- Look at some of the NLS weeks worked...