Q:

ValueError in onehotencoding

I'm not able to encode this column
Sex
male
female
female
female
male
male
male
male
female
female

from sklearn.preprocessing import LabelEncoder,OneHotEncoder
labelencoder=LabelEncoder()
X[:,2]=labelencoder.fit_transform(X[:,2])
ohe=OneHotEncoder(categorical_features=X[2])
ohe.fit_transform(X)

I'm getting this error.

could not convert string to float: 'male'

Can anyone help me with this?

A:

Demo:
In [6]: df
Out[6]:
      Sex
0    male
1  female
2  female
3  female
4    male
5    male
6    male
7    male
8  female
9  female

In [7]: le = LabelEncoder()

In [8]: df['Sex'] = le.fit_transform(df['Sex'])

In [9]: df
Out[9]:
   Sex
0    1
1    0
2    0
3    0
4    1
5    1
6    1
7    1
8    0
9    0

In [10]: df.dtypes
Out[10]:
Sex    int64
dtype: object

