transform categorical variables to numeric python code example
Example 1: pandas categorical to numeric
dataset["sex"] = dataset["sex"].astype('category').cat.codes
Example 2: transform categorical variables python
from sklearn.preprocessing import LabelEncoder
lb_make = LabelEncoder()
obj_df["make_code"] = lb_make.fit_transform(obj_df["make"])
obj_df[["make", "make_code"]].head(11)
Example 3: pandas categorical to numeric
dataset['Origin'] = dataset['Origin'].map({1: 'USA', 2: 'Europe', 3: 'Japan'})