Contents

Python Pandas For Your Grandpa - 3.13 Challenge: Cradle Robbers

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
## 3     Adam  Caitlin
## 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
## 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

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']
cradle_robbers = pd.concat((cr1, cr2))
print(cradle_robbers)
## 3      Adam
## 4    Rachel
## dtype: string

Course Curriculum

  1. Introduction
    1.1 Introduction
  2. Series
    2.1 Series Creation
    2.2 Series Basic Indexing
    2.3 Series Basic Operations
    2.4 Series Boolean Indexing
    2.5 Series Missing Values
    2.6 Series Vectorization
    2.7 Series apply()
    2.8 Series View vs Copy
    2.9 Challenge: Baby Names
    2.10 Challenge: Bees Knees
    2.11 Challenge: Car Shopping
    2.12 Challenge: Price Gouging
    2.13 Challenge: Fair Teams
  3. DataFrame
    3.1 DataFrame Creation
    3.2 DataFrame To And From CSV
    3.3 DataFrame Basic Indexing
    3.4 DataFrame Basic Operations
    3.5 DataFrame apply()
    3.6 DataFrame View vs Copy
    3.7 DataFrame merge()
    3.8 DataFrame Aggregation
    3.9 DataFrame groupby()
    3.10 Challenge: Hobbies
    3.11 Challenge: Party Time
    3.12 Challenge: Vending Machines
    3.13 Challenge: Cradle Robbers
    3.14 Challenge: Pot Holes
  4. Advanced
    4.1 Strings
    4.2 Dates And Times
    4.3 Categoricals
    4.4 MultiIndex
    4.5 DataFrame Reshaping
    4.6 Challenge: Class Transitions
    4.7 Challenge: Rose Thorn
    4.8 Challenge: Product Volumes
    4.9 Challenge: Session Groups
    4.10 Challenge: OB-GYM
  5. Final Boss
    5.1 Challenge: COVID Tracing
    5.2 Challenge: Pickle
    5.3 Challenge: TV Commercials
    5.4 Challenge: Family IQ
    5.5 Challenge: Concerts

Additional Content

  1. Python NumPy For Your Grandma
  2. Neural Networks For Your Dog
  3. Introduction To Google Colab