Skip to content

Instantly share code, notes, and snippets.

@marcosan93
Created August 28, 2021 17:40
Show Gist options
  • Select an option

  • Save marcosan93/34bd65053a2e6126629672f17695a4cd to your computer and use it in GitHub Desktop.

Select an option

Save marcosan93/34bd65053a2e6126629672f17695a4cd to your computer and use it in GitHub Desktop.
def getEducation(dob):
"""
Assigns an education level based on the given date of birth
"""
# Current date
now = datetime.datetime.now()
# Date of birth
dob = datetime.datetime.strptime(dob, "%Y-%m-%d")
# Subtracting the times to get an age
age = int((now - dob).days/365.25)
# Returning education level based on age
if age <= 18:
return 'high school'
elif age <= 22:
return 'undergrad'
elif age <= 25:
return 'grad'
else:
return 'employed'
df['education'] = [getEducation(i) for i in df['dob']]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment