To convert the Date of Birth to Age, we need to check the type of the column as below
train.info()
Method 1:
Here we can see that DOB column is of an object type
Step 1: We need to convert the DOB column as datetime type using below code
train['DOB']=pd.to_datetime(train['DOB'], format='%d-%b-%y')
train.info()
We can see that now the DOB column has been converted to datetime type
Step 2: To convert the DOB column to Age using below
from datetime import datetime
today=date.today()
now = pd.to_datetime('now')
train['Age']= (now.year - train['DOB'].dt.year) - ((now.month - train['DOB'].dt.month) < 0)
train.head()
We get a new column "Age" in the dataset
Method 2:
train['Age'] = train['DOB'].apply(lambda x: 119 - int(x[-2:]))
train['Age'].head()
Thank You!!
No comments:
Post a Comment