Contents

## Setup

Given a DataFrame of married `couples` and a separate DataFrame with each person’s age, identify “cradle robbers”, people who:

1. are at least 20 years older than their spouse and
2. who’s spouse is under the age of 30
``````import numpy as np
import pandas as pd

couples = pd.DataFrame({
'person1': ['Cody', 'Dustin', 'Peter', 'Adam', 'Ryan', 'Brian', 'Jordan', 'Gregory'],
'person2': ['Sarah', 'Amber', 'Brianna', 'Caitlin', 'Rachel', 'Kristen', 'Alyssa', 'Morgan']
}).convert_dtypes()
print(couples)
##    person1  person2
## 0     Cody    Sarah
## 1   Dustin    Amber
## 2    Peter  Brianna
## 4     Ryan   Rachel
## 5    Brian  Kristen
## 6   Jordan   Alyssa
## 7  Gregory   Morgan
``````
``````ages = pd.DataFrame({
'person': ['Adam', 'Alyssa', 'Amber', 'Brian', 'Brianna', 'Caitlin', 'Cody', 'Dustin', 'Gregory', 'Jordan',
'Kristen', 'Rachel', 'Morgan', 'Peter', 'Ryan', 'Sarah'],
'age': [62, 40, 41, 50, 65, 29, 27, 39, 42, 39, 33, 61, 43, 55, 28, 36]
}).convert_dtypes()
print(ages)
##      person  age
## 1    Alyssa   40
## 2     Amber   41
## 3     Brian   50
## 4   Brianna   65
## 5   Caitlin   29
## 6      Cody   27
## 7    Dustin   39
## 8   Gregory   42
## 9    Jordan   39
## 10  Kristen   33
## 11   Rachel   61
## 12   Morgan   43
## 13    Peter   55
## 14     Ryan   28
## 15    Sarah   36
``````

## Solution

``````ages = ages.set_index('person').age
couples['age1'] = ages.loc[couples.person1].to_numpy()
couples['age2'] = ages.loc[couples.person2].to_numpy()
cr1 = couples.loc[(couples.age1 - couples.age2 >= 20) & (couples.age2 < 30), 'person1']
cr2 = couples.loc[(couples.age2 - couples.age1 >= 20) & (couples.age1 < 30), 'person2']