Python 数组的Dataframe列到numpy数组

Python 数组的Dataframe列到numpy数组,python,pandas,numpy,Python,Pandas,Numpy,我有一个dataframe df,其中有一列包含长度为3的数组。现在,我想将此列转换为正确形状的numpy数组。但是,应用np.reforme不起作用。我该怎么做 下面是一个简单的例子: import pandas as pd import numpy as np df = pd.DataFrame(columns=['col']) for i in range(10): df.loc[i,'col'] = np.zeros(3) arr = np.array(df['col'])

我有一个dataframe df,其中有一列包含长度为3的数组。现在,我想将此列转换为正确形状的numpy数组。但是,应用np.reforme不起作用。我该怎么做

下面是一个简单的例子:

import pandas as pd
import numpy as np

df = pd.DataFrame(columns=['col'])
for i in range(10):
    df.loc[i,'col'] = np.zeros(3)

arr = np.array(df['col'])
np.reshape(arr, (10,3)) # This does not work

这里有两种使用np.vstack和np.concatenate的方法-

为了获得最佳性能,我们可以使用具有df.col.value的底层数据

样本运行-

In [116]: df
Out[116]: 
         col
0  [7, 5, 2]
1  [1, 1, 3]
2  [6, 1, 4]
3  [7, 0, 0]
4  [8, 8, 0]
5  [7, 8, 0]
6  [0, 5, 8]
7  [8, 3, 1]
8  [6, 6, 8]
9  [8, 2, 3]

In [117]: np.vstack(df.col)
Out[117]: 
array([[7, 5, 2],
       [1, 1, 3],
       [6, 1, 4],
       [7, 0, 0],
       [8, 8, 0],
       [7, 8, 0],
       [0, 5, 8],
       [8, 3, 1],
       [6, 6, 8],
       [8, 2, 3]])
In [116]: df
Out[116]: 
         col
0  [7, 5, 2]
1  [1, 1, 3]
2  [6, 1, 4]
3  [7, 0, 0]
4  [8, 8, 0]
5  [7, 8, 0]
6  [0, 5, 8]
7  [8, 3, 1]
8  [6, 6, 8]
9  [8, 2, 3]

In [117]: np.vstack(df.col)
Out[117]: 
array([[7, 5, 2],
       [1, 1, 3],
       [6, 1, 4],
       [7, 0, 0],
       [8, 8, 0],
       [7, 8, 0],
       [0, 5, 8],
       [8, 3, 1],
       [6, 6, 8],
       [8, 2, 3]])