Merge two arrays from one to two columns

a = np.array([1, 2, 3]) aa = np.array([1], [2], [3]) b = np.array([1, 2, 3]) bb = np.array([1], [2], [3]) np.concatenate((a, b), axis = 1) array([1, 2, 3, 1, 2, 3]) # It ok, that what I was expecting np.concatenate((a, b), axis = 0) array([1, 2, 3, 1, 2, 3]) # It not ok, that not what I was expecting 

I expected:

 array([[1, 2, 3], [1, 2, 3]]) 

even with aa and bb I get the same inconsistency. so is there a simple solution to combine two one-dimensional arrays along the 0 axis?

+4
source share
1 answer

Note that a and b are one-dimensional; no axis 1 for concatenation. Do you want vstack :

 >>> import numpy as np >>> a = np.array([1,2,3]) >>> b = a.copy() >>> np.vstack([a,b]) array([[1, 2, 3], [1, 2, 3]]) 

Alternatively, you can change a and b :

 >>> np.concatenate([a[np.newaxis,:],b[np.newaxis,:]],axis = 0) array([[1, 2, 3], [1, 2, 3]]) 
+5
source

All Articles