1

I have a problem with np.append(ar,values, axis=None)

Take a look at following codes:

import numpy as np

array1 = np.array([])
array2 = np.array([1,2])
array3 = np.array([3,4])
array4 = np.array([5,6])

array1 = np.append(array1, array2)
array1 = np.append(array1, array3)
array1 = np.append(array1, array4)

print(array1) # [1. 2. 3. 4. 5. 6.]

So this is not what i want i want add exactly the array to the array1 and the array1 should be

[[1. 2.]
 [3. 4.]
 [5. 6.]]

How can i do that?

3
  • you just want np.stack([array2, array3, array4]) Commented Feb 12, 2022 at 10:30
  • don't use np.append - unless you want to add just one number to a 1d array. Commented Feb 12, 2022 at 14:23
  • @amirhosseinzibaei is your issue solved? Commented Feb 13, 2022 at 9:39

2 Answers 2

1

You need to stack the arrays.

array1 = np.stack([array2, array3, array4])

should do it.

Sign up to request clarification or add additional context in comments.

2 Comments

ValueError: all input arrays must have the same shape
Corrected the answer, stack takes a list (or array like) of other arrays that you want to stack vertically.
0

Try this:

import numpy as np

array1 = np.array([])
array2 = np.array([1,2])
array3 = np.array([3,4])
array4 = np.array([5,6])
a = np.append(array1, array2)
b = np.append(array1, array3)
c = np.append(array1, array4)
print(np.vstack((a, b, c)))

output:

[[1. 2.]
 [3. 4.]
 [5. 6.]]

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.