Python-如何将数组添加到唯一的 numpy 数组?每个数组必须代表我的 numpy 数组中的一行

标签 python arrays numpy

我是 python 的初学者,我的问题是我有 4 个数组,其中有 x 个项目:

persons_id = [ 78694, 51203, ... ]
dates = [ '20072017', '19072017', ... ]
codes = [ 1500, 0606, ... ]
ranges = [ 70, 60, ... ]

我想做的是(在循环中)产生这种输出:

reporting = numpy.array([persons_id[0],
                        dates[0],
                        codes[0],
                        ranges[0]],
                        [persons_id[1],
                        dates[1],
                        codes[1],
                        ranges[1]],
                        [...])

提前谢谢

最佳答案

选项 1
np.vstack

np.vstack((persons_id, dates, codes, ranges)).T

array([['78694', '20072017', '1500', '70'],
       ['51203', '19072017', '606', '60']],
      dtype='<U21')

选项 2
np.stack(..., axis=1)

np.stack((persons_id, dates, codes, ranges), axis=1)

array([['78694', '20072017', '1500', '70'],
       ['51203', '19072017', '606', '60']],
      dtype='<U21')

关于Python-如何将数组添加到唯一的 numpy 数组?每个数组必须代表我的 numpy 数组中的一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46952742/

相关文章:

python - 将两个列表中所有可能的可乘结果矩阵放入数据框中

arrays - 给定两个无序列表,查询 A[i] > a 和 B[i] > b 的元素数

java - 如何通过 JNI 获取普通的一维数组而不是二维数组?

python - 无法导入 matplotlib

Python 格式将 numpy.float32 转换为 Python float

python - 在 Python 的 POST http header 中检索 HTML 表单数据

python - 如何在不覆盖当前内容的情况下写入文件?

python - 在 Django Channels 中重用现有的 websocket

arrays - Python 3.73 插入字节数组 = "object cannot be re-sized"

python - 在列表列表中查找前 N 个最频繁的数字序列