如何合并具有相同行数但不同列数的两个Numpy数组

时间:2020-02-28 19:42:35

标签: python

给出以下两个numpy数组:

# Two 2-dim arrays with same row number but differnt column
a1 = np.array([[9,9,9,9], [9,9,9,9],[9,9,9,9]], dtype=np.int64)
a2 = np.array([[3],[3],[3]], dtype=np.int64)

如何将它们合并到两个数组中,从而创建第三个数组,如下所示:

# Third array with same row numbers as above two arrays but its column number is the sum of column numbers of the above two arrays
[[9,9,9,9,3],
 [9,9,9,9,3],
 [9,9,9,9,3]]

简单来说,如何将一列连接到二维数组?

1 个答案:

答案 0 :(得分:1)

import numpy as np

a1 = np.array([[9,9,9,9], [9,9,9,9],[9,9,9,9]])
a2 = np.array([[3],[3],[3]])
print(np.concatenate((a1, a2), axis=1))

https://docs.scipy.org/doc/numpy/reference/generated/numpy.concatenate.html

Join a sequence of arrays along an existing axis.