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.