How can I calculate the age of a person (based off the dob column) and add a column to the dataframe with the new value?
dataframe looks like the following:
First thought is that your years are two digit, which is a not great choice in this day and age. In any case, I'm going to assume that all years like 05 are actually 1905. This is probably not correct(!) but coming up with the right rule is going to depend a lot on your data.
from datetime import date
def age(date1, date2):
naive_yrs = date2.year - date1.year
if date1.replace(year=date2.year) > date2:
correction = -1
else:
correction = 0
return naive_yrs + correction
df1['age'] = df1['dob'].map(lambda x: age(date(int('19' + x[-2:]), int(x[:2]), int(x[2:-2])), date.today()))