pandas convert text feature to numeric value
<p>I can convert all text features in a pandas dataframe by casting to 'category' using the df.astype() method as below. However I find category hard to work with (eg for plotting data) and would prefer to create a new column of integers</p>

<pre><code>#convert all objects to categories
object_types = dataset.select_dtypes(include=['O'])
for col in object_types:
    dataset['{0}_category'.format(col)] = dataset[col].astype('category')
</code></pre>

<p>I can convert the text to integers using this hack:</p>

<pre><code>#convert all objects to int values
object_types = dataset.select_dtypes(include=['O'])

new_cols = {}
for col in object_types:
    data_set = set(dataset[col].tolist())
    data_indexed = {}
    for i, item in enumerate(data_set):
        data_indexed[item] = i
    new_list = []
    for item in dataset[col].tolist():
        new_list.append(data_indexed[item])
    new_cols[col]=new_list

for key, val in new_cols.items():
    dataset['{0}_int_value'.format(key)] = val
</code></pre>

<p>But is there a better (or existing) way to do the same?</p>