简体   繁体   English

如何将一系列数组转换为2D numpy数组

[英]How to convert a Series of arrays into a 2D numpy array

Is there a way to convert a Pandas Series where each row contains an array into a 2D numpy array? 是否可以将每行包含一个数组的Pandas系列转换为2D numpy数组?

The Series looks like the following below when you display it in Python: 在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: 我想得到一个像这样的numpy矩阵:

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

Is there a simple way to do this? 有没有简单的方法可以做到这一点? Any help is appreciated! 任何帮助表示赞赏!

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.]])
l = [pd.Series([np.array([ 77.,  95.,  -1.,  -1.,  -1.]),np.array([ 58.,  -1.,  -1.,  -1.,  -1.])])]

If: 如果:

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

Then 然后

s.apply(pd.Series).values

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

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