Srivatsan
Srivatsan

Reputation: 9363

Numpy array (list of lists), add values at the end

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.

Upvotes: 0

Views: 1012

Answers (4)

MrNobody33
MrNobody33

Reputation: 6483

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]]

Upvotes: 2

Thulfiqar
Thulfiqar

Reputation: 392

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]]

Upvotes: 1

G. Anderson
G. Anderson

Reputation: 5955

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

Upvotes: 1

Quang Hoang
Quang Hoang

Reputation: 150735

You can do concatenate:

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

Output:

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

Upvotes: 1

Related Questions