encoding categorical data in python code example

Example 1: pandas categorical to numeric

#this will label the different catagories as 0,1,2,3....
dataset["sex"] = dataset["sex"].astype('category').cat.codes

Example 2: how to convert categorical data to binary data in python

MedInc        False
HouseAge      False
AveRooms      False
AveBedrms     False
Population    False
AveOccup      False
Latitude      False
Longitude     False
Price         False
dtype: bool

Example 3: pandas categorical to numeric

#this will label as one hot vectors (origin is split into 3 columns - USA, Europe, Japan and any one place will be 1 while the others are 0)
dataset['Origin'] = dataset['Origin'].map({1: 'USA', 2: 'Europe', 3: 'Japan'})