peeps
peeps

Reputation: 53

How to calculate age from a Dataframe column DOB(object type) and populate age in a new column Age using Pandas?

Let's say I have a data frame customer:

Name   DOB         Age?
A     12/10/1953
B     16/12/1980
C     20/01/1954
D     03/10/1961
E     13/05/1977

After calculating age I want to populate it in the Age column. Can anyone help in doing this?

Upvotes: 1

Views: 1950

Answers (1)

wwnde
wwnde

Reputation: 26676

Coerce DOB to datetime and subtract from today's date if you wanted current age in years.

import pandas as pd
import datetime as dt
df['Age?']=(dt.datetime.today()\
            -pd.to_datetime(df['DOB'])).astype('timedelta64[Y]')

Name         DOB  Age?
0    A  12/10/1953  66.0
1    B  16/12/1980  39.0
2    C  20/01/1954  66.0
3    D  03/10/1961  59.0
4    E  13/05/1977  43.0

Upvotes: 4

Related Questions