PriyankaJ
PriyankaJ

Reputation: 390

Python : Convert 2D numpy array to pandas series

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]

Upvotes: 2

Views: 1311

Answers (1)

jezrael
jezrael

Reputation: 862511

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

Upvotes: 2

Related Questions