converting non-numeric to numeric value using Panda libraries
<p>I am a machine learning beginner and wan't to learn ML using python and it's pandas module. So I have a Dataframe like this:</p>

<pre><code>COL1    COL2      COL3
a     9/8/2016     2
b     12/4/2016    23
         ...
n     1/1/2015     21
</code></pre>

<p>COL1 is a String, Col2 is a timestamp and Col3 is a number. Now I need to do some analysis on this Dataframe and I want to convert all the non-numeric data to numeric. I tried using <a href="http://scikit-learn.org/stable/modules/feature_extraction.html#dict-feature-extraction" rel="nofollow">DictVectorizer()</a> to convert COL1 and 2 to numeric but first of all I am not sure if this is the best way doing such a thing and second I don't know what to do with the timestamp.
When I use DictVectorizer the output would be like:</p>

<pre><code>{u'COL3: {0:2, 1:23 , ...,n:21}, 'COL1': {0: u'a', 1:'b', ... , n:'n'}, 'COL2': {0: u'9/8/2016' , 1: u'12/4/2016' , ... , n:u'1/1/2016'}}
</code></pre>

<p>but from what I learned it should be like this or at least I know I need something like this:</p>

<pre><code> {COL1:'a', COL2: '9/8/2016' , COL3: 2  and so on}   
</code></pre>

<p>so, questions:
1-what is the best way of converting non- numeric (including date) to numeric values to use in sklearn libraries
2- what is the right way of using DictVectorize()</p>

<p>Any help would be appreciated. </p>

<p>To encode non-numeric data to numeric you can use scikit-learn's <a href="http://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.LabelEncoder.html#sklearn.preprocessing.LabelEncoder" rel="nofollow">LabelEncoder</a>. It will encode each category such as COL1's <code>a</code>, <code>b</code>, <code>c</code> to integers.</p>

<p>Assuming df is your dataframe, try:</p>

<pre><code>from sklearn.preprocessing import LabelEncoder
enc = LabelEncoder()
enc.fit(df['COL1'])
df['COL1'] = enc.transform(df['col1'])
</code></pre>

<ul>
<li><code>enc.fit()</code> creates the corresponding integer values.</li>
<li><code>enc.transform()</code> applies the encoding to the df values.</li>
</ul>

<p>For the second column, using Pandas <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.to_datetime.html" rel="nofollow">to_datetime()</a> function should do the trick, like @quinn-weber mentioned, try:</p>

<pre><code>df['COL2'] = pd.to_datetime(df['COL2'])
</code></pre>

python pandas