Thursday, 31 October 2019

Convert Date Of Birth to Age in Dataframe



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!!


Wednesday, 30 October 2019

UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8b in position 2: invalid start byte

While reading the excel file you receive the below error

train = pd.read_csv("Train.csv")
train.head()


"UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8b in position 2: invalid start byte"


Include the encoding parameter in read_csv function as below

train = pd.read_csv("Train.csv",encoding='latin-1')


train.head()

now you will be able to read the file