2

Is there a way to convert a Pandas Series where each row contains an array into a 2D numpy array?

The Series looks like the following below when you display it in Python:

[array([ 58.,  -1.,  -1.,  -1.,  -1.])
 array([ 77.,  95.,  -1.,  -1.,  -1.])]

I would like to get a numpy matrix that looks like this:

[[ 58.,  -1.,  -1.,  -1.,  -1.]
 [ 77.,  95.,  -1.,  -1.,  -1.]]

Is there a simple way to do this? Any help is appreciated!

3 Answers 3

5
import pandas as pd
import numpy as np

s = pd.Series([np.array([ 58.,  -1.,  -1.,  -1.,  -1.]),
               np.array([ 77.,  95.,  -1.,  -1.,  -1.])])      

rslt = np.array(s.tolist())


rslt
Out[16]: 
array([[ 58.,  -1.,  -1.,  -1.,  -1.],
       [ 77.,  95.,  -1.,  -1.,  -1.]])
Sign up to request clarification or add additional context in comments.

Comments

2

If:

s = pd.Series([np.array([ 58.,  -1.,  -1.,  -1.,  -1.]),
               np.array([ 77.,  95.,  -1.,  -1.,  -1.])])

Then

s.apply(pd.Series).values

Comments

1
l = [pd.Series([np.array([ 77.,  95.,  -1.,  -1.,  -1.]),np.array([ 58.,  -1.,  -1.,  -1.,  -1.])])]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.