0

Using NumPy, I want to create an n-by-2 array, by starting with an empty array, and adding on some 1-by-2 arrays.

Here's what I have tried so far:

x = np.array([1, 2])
y = np.array([3, 4])
z = np.array([])
z = np.append(z, x)
z = np.append(z, y)

However, this gives me:

z = [1, 2, 3, 4]

What I want is:

z = [[1, 2], [3, 4]]

How can I achieve this?

2
  • np.append(z,x, axis=1)? Commented Oct 15, 2015 at 19:08
  • 1
    You are looking for np.row_stack. Commented Oct 15, 2015 at 19:09

2 Answers 2

2
import numpy as np

x = np.array([1, 2])
y = np.array([3, 4])
z = np.append([x],[y], axis=0)

print(z)

>>> [[1 2]
     [3 4]]

No need to create the array before appending, axis=0 will allow you to append row wise.

The previous works if z is not an array already. From then on you specify z as the original array and append the other array as such:

t = np.array([5, 6])
z = np.append(z,[t], axis=0)
print(z)

[[1 2]
 [3 4]
 [5 6]]
Sign up to request clarification or add additional context in comments.

Comments

1

You can simply use np.array :

>>> np.array((x,y))
array([[1, 2],
       [3, 4]])

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.