pythonpython-3.xpandasnumpynumpy-ndarray

How to convert a pandas column that contains numpy arrays to nd array


I have the following dataframe :

df = pd.DataFrame(
{'a':[np.array([1,2,3]),np.array([4,5,6])]
}
)

i want to have a numpy array like following extracted from the a column:

 array([[1., 2., 3.],
   [4., 5., 6.]])

i can do it like below:

 for i, row in df.iterrows():
     t_arr[i,:] = row["a"]

but i am looking for a solution without for loop. .values and to_numpy() don't return the desired results


Solution

  • Use np.stack to create a 2d numpy array.

    Either from the series object of your a column: np.stack(df['a'].values)

    Or from the whole DataFrame: np.stack(df.values.flatten())

    Edit: You don't even need to call .values. You can just call np.stack on the series object.