How to convert vector wrapped as string to numpy array in pandas dataframe?

Viewed 9318

I have a pandas dataframe with a column of vectors that I would like to perform matrix arithmetic on. However, upon closer inspection the vectors are all wrapped as strings with new line characters seemingly embedded in them:

enter image description here

How do I convert each vector in this column into numpy arrays? I've tried

df['Word Vector'].as_matrix

and

np.array(df['Word Vector'])

as well as

df['Word Vector'] = df['Word Vector'].astype(np.array)

but none produced the desired result. Any pointers would be appreciated!

3 Answers

This worked for me for string lists in a Pandas column:

df['Numpy Word Vector'] = df['Word Vector'].apply(eval).apply(np.array)

The solution below is shorter:

df[col_name] = df[col_name].apply(lambda x: np.array(eval(x)), 0)

Example:

df = pd.DataFrame(['[0., 1., 2., 3.]', '[1., 2., 3., 4.]'], columns=['Word Vector'])
df['Word Vector'][0] # '[0., 1., 2., 3.]'

df['Word Vector'] = df['Word Vector'].apply(lambda x: np.array(eval(x)), 0)
df['Word Vector'][0] # array([0., 1., 2., 3.])
Related