Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to append np.array?

I have an np.array d1 of (3,6), and an np.array a4 of (1,6).

How can I combine the two np.arrays to form an np.array d2 of (4,6)?

My code is as follows:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import numpy as np
a1=np.array(range(6))
a2=a1+2
a3=a2+3
a4=a3+4
d1=np.array([a1,a2,a3])
d1.shape
Out[44]: (3, 6)
d2=np.array([a1,a2,a3,a4])
d2.shape
d2
Out[45]: (4, 6)
Out[46]: 
array([[ 0,  1,  2,  3,  4,  5],
       [ 2,  3,  4,  5,  6,  7],
       [ 5,  6,  7,  8,  9, 10],
       [ 9, 10, 11, 12, 13, 14]])

How to get d2 from d1 and a4?

I tried np.insert and np.append, but maybe my usage is wrong and I didn’t get the correct result.

>Solution :

I believe https://numpy.org/doc/stable/reference/generated/numpy.concatenate.html concatenate is the command you are looking for.

foo = np.array([[1,2,3],
                [4,5,6]])
bar = np.array([7,8,9])

# axis 0 will combine rows, axis 1 will combine columns
foobar = numpy.concatenate(foo,bar,axis = 0)

array([[1,2,3],
       [4,5,6],
       [7,8,9]])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading