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.
ast.literal_eval(x)first. Do it on the entire column to make this easier:df.segment = df.segment.apply(ast.literal_eval)