I am converting strings to categorical values in my dataset using the following piece of code.
data[\'weekday\'] = pd.Categorical.from_array(data.weekday).labels
If you have numerical and categorical both type of data in dataframe You can use : here X is my dataframe having categorical and numerical both variables
from sklearn import preprocessing
le = preprocessing.LabelEncoder()
for i in range(0,X.shape[1]):
if X.dtypes[i]=='object':
X[X.columns[i]] = le.fit_transform(X[X.columns[i]])
Or you can try this:
from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
data = data.apply(le.fit_transform)
Note: This technique is good if you are not interested in converting them back.
The best way of doing this can be to use label encoder of sklearn library.
Something like this:
from sklearn import preprocessing
le = preprocessing.LabelEncoder()
le.fit(["paris", "paris", "tokyo", "amsterdam"])
list(le.classes_)
le.transform(["tokyo", "tokyo", "paris"])
list(le.inverse_transform([2, 2, 1]))