So I have a dataframe column that includes a numpy
array, but its read in as a string. So, I end up with the following as a single element:
df['numpy_arr'].iloc[0] = ' 2 3 5 23 5 2 23 '
I want to convert this to a numpy array, and have successfully done so for a single instance using numpy.fromstring
:
first = df['numpy_arr'].iloc[0]
np.fromstring(first, sep=' ')
However, when I try to generalize this using the apply
:
df['numpy_arr'].apply(lambda x: np.fromstring(x, sep=' '))
It returns an empty series. Why is this the case? Is it because the x
in the lambda doesn't actually refer to the actual string? I also tried the following based off of this:
[np.fromstring(vector,sep=' ') for vector in df['numpy_arr']]
Which again returns empty arrays. Why is this the case? How can I generalize this to work on the whole series and convert the elements to a numpy array?
[EDIT] As a last resort, I suppose I can iterate across .iloc[x]
but that seems like a very inefficient way to do this, especially since I would have to transform it back into a series