python strings to int/float in an efficient way
Question: I have an array (in numpy, or in pandas) containing (non-unique) strings. Some
of them are ints written as strings, some comprise of both digits and letters.
What I would like to do is to map these strings onto (some) int or float
values, in order to process them further.

I don't mean simple int(string,base). I mean a procedure that would, say go
through all the strings, and then say "Aha, so lets's assign to this string
such and such 'int/float-key'".

What's the most efficient way of doing that?


Answer: It sounds like you have a pandas DataFrame with various strings that you want
to convert to indexed values such that each unique string has a unique integer
value.

`numpy.unique` does what you need. (You already mentioned that you were using
numpy, so I'm going to post a numpy solution.)

For example:

    
    
    import numpy as np
    import pandas
    
    df = pandas.DataFrame(dict(x=['1', 'a5', 'cde9', '1', 'cde9']))
    
    unique_vals, df['keys'] = np.unique(df.x, return_inverse=True)
    
    print df
    

