Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to convert a Series of arrays into a single matrix in pandas/numpy?

Tags:

I somehow got a pandas.Series which contains a bunch of arrays in it, as the s in the code below.

data = [[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]] s = pd.Series(data = data) s.shape # output ---> (11L,) # try to convert s to matrix sm = s.as_matrix() # but... sm.shape # output ---> (11L,) 

How can I convert the s into a matrix with shape (11,3)? Thanks!

like image 385
user3768495 Avatar asked Nov 27 '16 00:11

user3768495


People also ask

How do you convert a series to a matrix in Python?

as_matrix() function is used to convert the given series or dataframe object to Numpy-array representation.

How do you convert an array to a matrix in Python?

Use the numpy. array() method to convert list to matrix in Python. Using the numpy. asarray() method to convert list to matrix in Python.

How can you convert a series object to a NumPy array?

to_numpy() Pandas Series. to_numpy() function is used to return a NumPy ndarray representing the values in given Series or Index. This function will explain how we can convert the pandas Series to numpy Array.


2 Answers

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.

like image 52
Selah Avatar answered Sep 23 '22 03:09

Selah


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]: s Out[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: object  In [17]: sm = np.array(s.tolist())  In [18]: sm Out[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.shape Out[19]: (11, 3) 

But unless it's something you can't change, having that Series makes little sense to begin with.

like image 37
juanpa.arrivillaga Avatar answered Sep 24 '22 03:09

juanpa.arrivillaga