how to convert categorical data to numerical data in python pandas code example
Example 1: pandas categorical to numeric
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: 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 4: pandas categorical to numeric
dataset['Origin'] = dataset['Origin'].map({1: 'USA', 2: 'Europe', 3: 'Japan'})
Example 5: how to convert categorical data to numerical data in python
pd.get_dummies(obj_df, columns=["body_style", "drive_wheels"], prefix=["body", "drive"]).head()