Pandas get the age from a date (example: date of birth)

后端 未结 6 1187
夕颜
夕颜 2020-12-01 06:33

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:

6条回答
  •  长情又很酷
    2020-12-01 07:31

    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()))
    

提交回复
热议问题