how to convert a Series of arrays into a single matrix in pandas/numpy?
Another way is to extract the values of your series and use numpy.stack on them.
np.stack(s.values)
PS. I've run into similar situations often.
If, for some reason, you have found yourself with that abomination of a Series
, getting it back into the sort of matrix
or array
you want is straightforward:
In [16]: sOut[16]:0 [1, 2, 3]1 [2, 3, 4]2 [3, 4, 5]3 [2, 3, 4]4 [3, 4, 5]5 [2, 3, 4]6 [3, 4, 5]7 [2, 3, 4]8 [3, 4, 5]9 [2, 3, 4]10 [3, 4, 5]dtype: objectIn [17]: sm = np.array(s.tolist())In [18]: smOut[18]:array([[1, 2, 3], [2, 3, 4], [3, 4, 5], [2, 3, 4], [3, 4, 5], [2, 3, 4], [3, 4, 5], [2, 3, 4], [3, 4, 5], [2, 3, 4], [3, 4, 5]])In [19]: sm.shapeOut[19]: (11, 3)
But unless it's something you can't change, having that Series makes little sense to begin with.
For the pandas>=0.24, you can also np.stack(s.to_numpy())
or np.concatenate(s.to_numpy())
, depending on your requirement.