Reputation: 303
My data frame looks like -
id dob
1 13/01/1978
2 03/08/1957
3 22/12/1977
I want to calculate age based on 'dob' column.
id dob age
1 13/01/1978 43
2 03/08/1957 64
3 22/12/1976 44
I have done the below code but its not working
now = datetime.datetime.now()
data['dob'] = pd.to_datetime(data['dob'])
data['age'] = (now.date() - data['dob']).astype('<m8[Y]')
Upvotes: 0
Views: 335
Reputation: 1093
Here is a solution:
d = { 'Id': [1,2,3],
'dob': ['13/01/1978', '03/08/1957', '22/12/1977'] }
df = pd.DataFrame(d)
df['dob']= pd.to_datetime(df['dob'])
now = datetime.datetime.now()
df['age'] = df['dob'].apply(lambda x: now.year - x.year)
#Output:
Id dob age
0 1 1978-01-13 43
1 2 1957-03-08 64
2 3 1977-12-22 44
This version is more precise because based on days instead of year:
df['age_2'] = df['dob'].apply(lambda x: int((now - x).days / 365.25) )
#Output:
Id dob age age_2
0 1 1978-01-13 43 43
1 2 1957-03-08 64 64
2 3 1977-12-22 44 43
Upvotes: 1