Description of the problem:
I have an array-like structure in a dataframe column as a string (I read the dataframe from a csv file).
One string element of this column looks like this:
In [1]: df.iloc[0]['points']
Out [2]: '[(-0.0426, -0.7231, -0.4207), (0.2116, -0.1733, -0.1013), (...)]'
so it's really an array-like structure, which looks 'ready for numpy' to me.
numpy.fromstring() doesn't help as it doesn't like brackets:
convert string representation of array to numpy array in python
A simple numpy.array() on the string itself, if I copy and paste it in the array() function is returning me a numpy array.
But if I fill the array() function with the variable containing the string like that: np.array(df.iloc[0]['points']) it does not work, giving me a ValueError: could not convert string to float
The question:
Is there any function to do that in a simple way (without replacing or regex-ing the brackets)?
ast.literal_evalon each string. That will work with the example you posted, but no guarantee that it will work with all of your data, or that you won’t get float rounding problems that wouldn’t be there with properly serialized data. It’s a hack, not a solution.structuredarray is a list of tuples. Thereprstring will includedtypeinformation. But since this is a cell in a DataFrame, there may be other possibilites. Did you load thisdffrom acsvfile? Are there quote strings like this in that file?