0

hi I have 3 array and I wanna merge them element by element.for example

a=array([3.1, 3.3])
b=array([4.1, 4.3])
c=array([6.1, 1.3])

how I merge add them to get this

array([3.1, 4.1,6.1],[3.3,4.3,1.3])
  • Does this answer your question? [Merge two arrays vertically to array of tuples using numpy](https://stackoverflow.com/questions/35091879/merge-two-arrays-vertically-to-array-of-tuples-using-numpy) – bluejambo Aug 03 '21 at 14:18
  • What have you tried? `np.array((a,b,c))`? – hpaulj Aug 03 '21 at 15:20

2 Answers2

1

You could first stack the arrays on top of each other

a=np.array([3.1, 3.3])
b=np.array([4.1, 4.3])
c=np.array([6.1, 1.3])

stacked = np.vstack([a, b, c])

and then transpose the result

result = stacked.T

which gives

>>> result
array([[3.1, 4.1, 6.1],
       [3.3, 4.3, 1.3]])
user8408080
  • 2,428
  • 1
  • 10
  • 19
0
In [374]: a=np.array([3.1, 3.3])
     ...: b=np.array([4.1, 4.3])
     ...: c=np.array([6.1, 1.3])

A common way to make a new array from others is with np.array:

In [375]: np.array((a,b,c))
Out[375]: 
array([[3.1, 3.3],
       [4.1, 4.3],
       [6.1, 1.3]])

Those are the numbers; just the layout is different, (3,2) as opposed to its transpose.

Or use variations on concatenate/stack. For example:

In [380]: np.column_stack((a,b,c))
Out[380]: 
array([[3.1, 4.1, 6.1],
       [3.3, 4.3, 1.3]])

In [381]: np.stack((a,b,c),axis=1)
Out[381]: 
array([[3.1, 4.1, 6.1],
       [3.3, 4.3, 1.3]])

Or even one call to transpose (though under the cover this first does the [375] step):

In [382]: np.transpose((a,b,c))
Out[382]: 
array([[3.1, 4.1, 6.1],
       [3.3, 4.3, 1.3]])

These functions are all documented.

hpaulj
  • 221,503
  • 14
  • 230
  • 353