90

I have two numpy 1d arrays, e.g:

a = np.array([1,2,3,4,5])
b = np.array([6,7,8,9,10])

Then how can I get one 2d array [[1,6], [2,7], [3,8], [4,9], [5, 10]]?

1
  • 4
    For very small arrays, zip can be faster than calling Numpy functions, but for longer arrays the Numpy functions are much faster, with column_stack being the fastest in my tests. Eg, for arrays of length 1000, column_stack is about 1000 times faster than zip. Here's some relevant timeit code. Commented Jun 8, 2017 at 10:49

3 Answers 3

182

If you have numpy arrays you can use dstack():

import numpy as np

a = np.array([1,2,3,4,5])
b = np.array([6,7,8,9,10])

c = np.dstack((a,b))
#or
d = np.column_stack((a,b))

>>> c
array([[[ 1,  6],
        [ 2,  7],
        [ 3,  8],
        [ 4,  9],
        [ 5, 10]]])
>>> d
array([[ 1,  6],
       [ 2,  7],
       [ 3,  8],
       [ 4,  9],
       [ 5, 10]])

>>> c.shape
(1, 5, 2)
>>> d.shape
(5, 2)
Sign up to request clarification or add additional context in comments.

4 Comments

Alternatively, np.stack((a, b), axis=-1), but your way is more compact.
FWIW, np.column_stack appears to be the fastest way to do this, see my comment on the question for details.
Just a note - np.dstack stacks arrays in the 3rd dimension. thus you'd be creating an unnecessary dimension. np.column_stack, or np.stack(...,axis=-1) are both more appropriate.
Why axis=-1 and not +1?
55

The answer lies in your question:

np.array(list(zip(a,b)))

Edit:

Although my post gives the answer as requested by the OP, the conversion to list and back to NumPy array takes some overhead (noticeable for large arrays).

Hence, dstack would be a computationally efficient alternative (ref. @zipa's answer). I was unaware of dstack at the time of posting this answer so credits to @zipa for introducing it to this post.

Edit 2:

As can be seen in the duplicate question, np.c_ is even shorter than np.dstack.

>>> import numpy as np
>>> a = np.arange(1, 6)
>>> b = np.arange(6, 11)
>>> 
>>> a
array([1, 2, 3, 4, 5])
>>> b
array([ 6,  7,  8,  9, 10])
>>> np.c_[a, b]
array([[ 1,  6],
       [ 2,  7],
       [ 3,  8],
       [ 4,  9],
       [ 5, 10]])

3 Comments

Why use plain Python functions to do this when you're working with Numpy arrays?
@PM2Ring You're right. I was unaware of zipa's answer but was the first to post.
I don't like someone mixing two types of list when doing operations.
3

You can use zip

np.array(list(zip(a,b)))
array([[ 1,  6],
   [ 2,  7],
   [ 3,  8],
   [ 4,  9],
   [ 5, 10]])

4 Comments

how is this different to @Ebe's answer?
Well we both answered at the same time, i was almost 2-3 seconds late
Why use plain Python functions to do this when you're working with Numpy arrays?
sure, i will go with @zipa's answer

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.