Skip to content

Cradle Robbers Problem


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

  • who are at least 20 years older than their spouse and
  • whose 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()

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(couples)
#    person1  person2
# 0     Cody    Sarah
# 1   Dustin    Amber
# 2    Peter  Brianna
# 3     Adam  Caitlin
# 4     Ryan   Rachel
# 5    Brian  Kristen
# 6   Jordan   Alyssa
# 7  Gregory   Morgan

print(ages)
#      person  age
# 0      Adam   62
# 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

Try with Google Colab