Using LabelEncoder for a series in scikitlearn
<p>I have a Column in a Dataset which has categorical values and I want to convert them in Numerical values. I am trying to use LabelEncoder but get errors doing so.</p>

<pre><code>from sklearn.preprocessing import LabelEncoder
m = hsp_train["Alley"]
m_enc = LabelEncoder()
j = m_enc.fit_transform(m)
</code></pre>

<p>I am getting an error: </p>

<blockquote>
  <p>unorderable types: float() > str()</p>
</blockquote>

<p>The series in the Column has 3 values. I want them to be 0, 1, 2 respectively but I am getting that error.</p>

<p>I also tried this:</p>

<pre><code>l = hsp_train["Alley"]
l_enc = pd.factorize(l)
hsp_train["Alley"] = l_enc[0]
</code></pre>

<p>But this is giving me values -1, 1, 2. which I don't want I want it from 1.</p>

<p>It's obviously clear that you have missing values in your series. If you want to remove <code>NaN</code> values from your series, just do <code>hsp_train["Alley"].dropna()</code></p>

<p><strong>Illustration:</strong></p>

<pre><code>df = pd.DataFrame({'Categorical': ['apple', 'mango', 'apple', 
                                   'orange', 'mango', 'apple', 
                                   'orange', np.NaN]})
</code></pre>

<p>Using <code>LabelEncoder</code> to encode the categorical labels:</p>

<pre><code>enc = LabelEncoder()
enc.fit_transform(df['Categorical'])
</code></pre>

<p>Gives:</p>

<blockquote>
  <p>TypeError: unorderable types: float() > str()</p>
</blockquote>

<p>Doing <code>pd.factorize</code> automatically assigns -1 to missing values by default and hence you get those values:</p>

<pre><code>pd.factorize(df['Categorical'])[0]
array([ 0,  1,  0,  2,  1,  0,  2, -1])
</code></pre>

<hr>

<p>If you do not want <code>NAN</code> values to be identified and to consider them just as any string, you can do it while reading process using <code>na_filter</code>: </p>

<pre><code>df = pd.read_csv(data, na_filter=False, ...)
</code></pre>

<p>It also improves the performance of reading a relatively large file drastically.</p>

<hr>

<p>Or, you could fill all the <code>NaN</code> values using <code>fillna</code> to the desired string of your choice:</p>

<pre><code>df.fillna('Na', inplace=True)
</code></pre>

<p>This replaces all the <code>NaN</code> values to your string value "Na" and you can continue as before.</p>

python pandas machine-learning scikit-learn