简体   繁体   中英

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]

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM