How to append two arrays in NumPy

You can append two arrays in the NumPy with the following code. If you want to learn Python, I highly recommend reading This Book.

How to append two arrays in NumPy
How to append two arrays in NumPy

Example 1

import numpy as np

a = np.array([5, 10, 15])
b = np.array([1, 2, 3])

c = np.append(a, b)
print(c)

Output

[ 5 10 15  1  2  3]

Example 2

import numpy as np

a = np.array([[5, 10, 15],[20, 25, 30]])
b = np.array([[1, 2, 3],[4, 5, 6]])

c = np.append(a, b)
print(c)
[ 5 10 15 20 25 30  1  2  3  4  5  6]

Example 3

import numpy as np

a = np.array([[5, 10, 15],[20, 25, 30]])
b = np.array([[1, 2, 3],[4, 5, 6]])

c = np.append(a, b,axis=0)
print(c)
[[ 5 10 15]
 [20 25 30]
 [ 1  2  3]
 [ 4  5  6]]

Example 4

import numpy as np

a = np.array([[5, 10, 15],[20, 25, 30]])
b = np.array([[1, 2, 3],[4, 5, 6]])

c = np.append(a, b,axis=1)
print(c)
[[ 5 10 15  1  2  3]
 [20 25 30  4  5  6]]

People are also reading:

Best Python Books

What is Computer Vision? Examples, Applications, Techniques

Books for Machine Learning (ML)

Free Learning Resources

Leave a Comment

Your email address will not be published.