LabelEncoder().fit_transform vs. pd.get_dummies for categorical coding
<p>It was recently brought to my attention that if you have a dataframe <code>df</code> like this:</p>

<pre><code>   A      B   C
0  0   Boat  45
1  1    NaN  12
2  2    Cat   6
3  3  Moose  21
4  4   Boat  43
</code></pre>

<p>You can encode the categorical data automatically with <code>pd.get_dummies</code>:</p>

<pre><code>df1 = pd.get_dummies(df)
</code></pre>

<p>Which yields this:</p>

<pre><code>   A   C  B_Boat  B_Cat  B_Moose
0  0  45     1.0    0.0      0.0
1  1  12     0.0    0.0      0.0
2  2   6     0.0    1.0      0.0
3  3  21     0.0    0.0      1.0
4  4  43     1.0    0.0      0.0
</code></pre>

<p>I typically use <code>LabelEncoder().fit_transform</code> for this sort of task before putting it in <code>pd.get_dummies</code>, but if I can skip a few steps that'd be desirable.  </p>

<p>Am I losing anything by simply using <code>pd.get_dummies</code> on my entire dataframe to encode it?  </p>

<p>Yes, you can skip the use of <code>LabelEncoder</code> if you only want to encode string features. On the other hand if you have a categorical column of integers (instead of strings) then <code>pd.get_dummies</code> will leave as it is (see your A or C column for example). In that case you should use <a href="http://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.OneHotEncoder.html" rel="nofollow"><code>OneHotEncoder</code></a>. Ideally <code>OneHotEncoder</code> would support both integer and strings but this is being <a href="https://github.com/scikit-learn/scikit-learn/pull/7327" rel="nofollow">worked on at the moment</a>.</p>

python pandas scikit-learn sklearn-pandas