Cabbage Champion
Cabbage Champion

Reputation: 1213

How to merge two numpy arrays with same number of rows but different number of columns

Given these two numpy arrays:

# 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)

How would one merge these to arrays two create a third array like the following:

# 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]]

In simpler terms, how do you concatenate a column to a 2-dimensional array?

Upvotes: 1

Views: 2816

Answers (1)

ofeks
ofeks

Reputation: 107

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.

Upvotes: 1

Related Questions