2

I have a 2D numpy array like below, which I would like to store in a pandas column. I'm unable to find relevant example any where upon search. Please help.Thanks!

#2D array
[[0,2],[2,3]]

#Expected output is pandas dataframe with single column :
[0,2]
[2,3]
1
  • pd.Series(data) Commented Apr 17, 2020 at 10:13

1 Answer 1

2

IIUC use:

a = np.array([[0,2],[2,3]])
s = pd.Series(a.tolist())
print (s)
0    [0, 2]
1    [2, 3]
dtype: object

EDIT: For avoid lists use:

a = np.array([[0,2],[2,3]])
s = pd.Series(list(a))
print (s)
0    [0, 2]
1    [2, 3]
dtype: object

print (s.apply(type))
0    <class 'numpy.ndarray'>
1    <class 'numpy.ndarray'>
dtype: object
Sign up to request clarification or add additional context in comments.

2 Comments

But how to do this if I want the elemnts to remain numpy arrays, not python lists?
@Toby - added better simplier solution.

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.