简体   繁体   English

Python:将二维 numpy 数组转换为 pandas 系列

[英]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.我有一个 2D numpy 数组,如下所示,我想将其存储在 pandas 列中。 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: IIUC 用途:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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