Pandas convert categorical into numeric 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: panda categorical data into numerica

sex = train_dataset['Sex'].replace(['female','male'],[0,1])
print(sex)

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'})

Example 4: how to convert categorical data to numerical data in python

obj_df["body_style"] = obj_df["body_style"].astype('category')
obj_df.dtypes