0

I have two array's:

In [32]: a                                                                      
Out[32]: 
array([[1, 2, 3],
       [2, 3, 4]])

In [33]: b                                                                      
Out[33]: 
array([[ 8,  9],
       [ 9, 10]])

I would like to get the following:

In [35]: c                                                                      
Out[35]: 
array([[ 1,  2,  3,  8,  9],
       [ 2,  3,  4,  9, 10]])

i.e. apped the first and second value of b[0] = array([8, 9]) as the last two values of a[0] and append the first and second value of b[1] = array([9,10]) as the last two values of a[1].

The second answer in this link: How to add multiple extra columns to a NumPy array does not work and I do not understand the accepted answer.

4 Answers 4

2

You could try with np.hstack:

a=np.array([[1, 2, 3],
           [2, 3, 4]])
    

b=np.array([[ 8,  9],
       [ 9, 10]])
print(np.hstack((a,b)))

output:

[[ 1  2  3  8  9]
 [ 2  3  4  9 10]]

Or since the first answer of link you attached is faster than concatenate, and as you can see G.Anderson's timings, the fastest was concatenate, here is an explanation, so you can use that first answer:

#So you create an array of the same shape that the expected concatenate output:
res = np.zeros((2,5),int)
res
[[0 0 0 0 0]
 [0 0 0 0 0]]

#Then you assign res[:,:3] to fisrt array, where res[:,:3] that is the first 3 elements of each row
res[:,:3]
[[0 0 0]
 [0 0 0]]
res[:,:3]=a   #assign
res[:,:3]
[[1, 2, 3]
 [2, 3, 4]]


#Then you assign res[:,3:] to fisrt array, where res[:,3:] that is the last two elements of eah row
res[:,3:]
[[0 0]
 [0 0]]
res[:,3:]=b  #assign
res[:,3:]
[[ 8,  9]
 [ 9, 10]]

#And finally:
res
[[ 1  2  3  8  9]
 [ 2  3  4  9 10]]
Sign up to request clarification or add additional context in comments.

Comments

1

You can do concatenate:

np.concatenate([a,b], axis=1)

Output:

array([[ 1,  2,  3,  8,  9],
       [ 2,  3,  4,  9, 10]])

Comments

1

You can use np.append with the axis parameter for joining two arrays on a given axis

np.append(a,b, axis=1)

array([[ 1,  2,  3,  8,  9],
       [ 2,  3,  4,  9, 10]])

Adding timings for the top three answers, for completeness sake. Note that these timings will vary based on the machine running the code, and may scale at different rates for different sizes of array

%timeit np.append(a,b, axis=1)
2.81 µs ± 438 ns per loop

%timeit np.concatenate([a,b], axis=1)
2.32 µs ± 375 ns per loop

%timeit np.hstack((a,b))
4.41 µs ± 489 ns per loop

1 Comment

I discourage the use of np.append. It's too tempting to treat it like a list append clone, which it is not.
1

from numpy documentation about numpy.concatenate

Join a sequence of arrays along an existing axis.

and from the question, I understood is that what you want

import numpy as np

a = np.array([[1, 2, 3],
       [2, 3, 4]])

b = np.array([[ 8,  9],
       [ 9, 10]])

c = np.concatenate((a, b), axis=1)

print ("a: ", a)
print ("b: ", b)
print ("c: ", c)

output:

a:  [[1 2 3]
 [2 3 4]]

b:  [[ 8  9]
 [ 9 10]]

c:  [[ 1  2  3  8  9]
 [ 2  3  4  9 10]]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.