Converting numpy ndarray with dtype <U30 into float

Viewed 1930

I'm reading a list from pandas dataframe cell.

>>from pandas import DataFrame as table
>>x = table.loc[table['person'] == int(123), table.columns != 'xyz']['segment'][0]
>>print("X = ",x)

where 'person' and 'segment' are my column names and segment contains a list with floating values.

>>X = [[39.414, 39.498000000000005]]

Now, when I try to convert this into a numpy array,

>>x = numpy.asarray(x)
>>x=x.astype(float)

I get the following error

ValueError: could not convert string to float: '[[39.414, 39.498000000000005]]'

I have tried parsing the string and tried to remove any "\n" or " " or any unnecessary quotes, but it does not work. Then I tried to find the dtype

>>print("Dtype = ", x.dtype)
>>Dtype = <U30

I assume that we need to convert the U30 dtype into floats, but I am not sure how to do it. I am using numpy version 1.15.0.

All I want to do is, to parse the above list into a list with floating point values.

2 Answers

The datatype should have tipped you off. U30 here stands for a length 30 unicode string (Which is what you'll see if you type len(x).

What you have is the string representation of a list, not a list of strings/floats/etc..

You need to use the ast library here:

x = '[[39.414, 39.498000000000005]]'
x = ast.literal_eval(x)
np.array(x, dtype=float)

array([[39.414, 39.498]])

For the specific format you see, consider np.fromstring. With string slicing you can also remove the unused dimension:

x = '[[39.414, 39.498000000000005]]'

res = np.fromstring(x[2:-2], sep=',')

# array([ 39.414,  39.498])
Related