2

I have a pandas frame like this.

pd.DataFrame(data={'name':['name1','name2'],'vector':[np.array([1,2,3,4]),np.array([12,22,34,4])]})

I want to extract the vectors from the frame as a matrix like this.

np.array([[1,2,3,4],[12,22,34,4]])
tanjing
  • 81
  • 2
  • 8

2 Answers2

3
np.array(df['vector'].tolist())

will result in

array([[ 1,  2,  3,  4],
       [12, 22, 34,  4]])

or

df['vector'].as_matrix()

will result in

array([array([1, 2, 3, 4]), array([12, 22, 34,  4])], dtype=object)
plasmon360
  • 4,109
  • 1
  • 16
  • 19
2

df.vector.values should be the shortest.