Skip to content Skip to sidebar Skip to footer

Pandas Calculating Age From A Date

I really need help with this one. My previous post was very bad and unclear - I'm sorry - I wish I could delete but hopefully this one will be better. I need to calculate the age

Solution 1:

Convert the dob column from string to a datetime object

df1['dob'] = pd.to_datetime(df1['dob'])
now = datetime.now()    
df1['age'] = now - df1['dob']

Solution 2:

Convert string to datetime with format

df1['age'] = now - datetime.strptime(df1['dob'], "%m%d%Y")

Solution 3:

if there's not too many entries, you can just do something like:

df['dob'] = df.dob.apply(lambda d: pd.to_datetime(d[-4:] + d[:4]))
now - df.dob

Post a Comment for "Pandas Calculating Age From A Date"