1.
preprocessing.LabelEncoder()
import pandas as pd from sklearn import preprocessing le = preprocessing.LabelEncoder() ddf = pd.DataFrame({"id":[1,2,3,1],"name":["a","b","c","a"],"age":[7,8,9,10]}) ddf[["id","name"]] = ddf[["id","name"]].fillna(0) k = le.fit_transform(ddf["name"]) ddf["name"] = k ddf
2.
ddf["name"] = ddf["name"].astype("category").cat.codes