How to Concatenate NumPy Arrays in Python?

In this article, we will learn how to concatenate a NumPy array to another NumPy array in python.

Table Of Contents

Given two NumPy arrays, we need to add the 2nd array at the end of first array.

Example:             

1st NumPy Array = [ 1  2  3  4  5 ]
1st NumPy Array = [ 6  7  8  9 10 ]

After concatenating two NumPy Arrays, the final NumPy Array must be like:
[ 1  2  3  4  5  6  7  8  9 10]
    

There are multiple ways to concatenate a NumPy array to another NumPy array. Let’s discuss all the methods one by one with proper approach and a working code example

1.) Using append() method to concatenate NumPy Arrays

Numpy module in python, provides a function numpy.append() to add objects to the end of an array, The object should be an array like. The append method will take array, object to be appended as arguments. It will append the given object at the end of the array and returns the array.

Syntax of append()

numpy.append(arr, values, axis=None)

Parameters:

arr          = The array to be passed to the function.
values       = array_like object to appended to array.
axis         = int, optional, Axis along which to append values.

Return:

Returns array with values appended at end.    

In this case, to concatenate two numpy arrays we need to pass two arrays to the append() method. It will return the concatenated array.

Advertisements

Approach

  1. Import numpy library and create two numpy arrays
  2. Now pass the array1, array2 to the append() method.
  3. The method will return return of a copy of the concatenated array.
  4. Print the concatenated array

Source code

import numpy as np

# create numpy arrays
arr1 = np.array([1, 2, 3, 4, 5])
arr2 = np.array([6, 7, 8, 9, 10])

#concatenating two arrays using append() method
arr = np.append(arr1, arr2)

# printing the concatenated array
print(" The concatenated array is = ", arr)
        
    

OUTPUT:

The concatenated array is =  [ 1  2  3  4  5  6  7  8  9 10]

2.) Using concatenate() method to concatenate NumPy Arrays

Numpy module in python, provides a function numpy.concatenate() to join a sequence of arrays along an existing axis. The concatenate method will take a sequence of arrays as parameters. It will concatenate the arrays into one single array and returns the concatenated array.

Now to concatenate two arrays, In the sequence of arrays we will pass the given two arrays, The concatenate() method will return the concatenated array.

Example

concatenate(( [1 2 3 4 5 6] , [6 7 8 9 10] )) ==> [ 1 2 3 4 5 6 7 8 9 10]

Syntax of concatenate()

numpy.concatenate((a1, a2, ...), axis=0)

Parameters:

(a1, a2, ...) = Sequence of arrays to be passed to the function.
axis          = int, optional, Axis along which to concatenate arrays.

Return:

Returns a concatenated array.    

Approach

  • Import numpy library and create two numpy arrays
  • Now pass the array1, array2 as a sequence of arrays to the concatenate method
  • The method will return return of a copy of the concatenated array.
  • Print the concatenated array

Source code

import numpy as np

# create numpy arrays
arr1 = np.array([1, 2, 3, 4, 5])
arr2 = np.array([6, 7, 8, 9, 10])

#concatenating two arrays using concatenate() method
arr = np.concatenate((arr1, arr2))

# printing the concatenated array
print(" The concatenated array is = ", arr)
        
    

OUTPUT:

The concatenated array is =  [ 1  2  3  4  5  6  7  8  9 10]

3.) Using hstack() method to Concatenate NumPy Arrays

Numpy module in python, provides a function numpy.hstack() function is used to stack the sequence of input arrays horizontally i.e, concatenating into a single array. The hstack method will take a sequence of arrays as parameters. It will concatenate the arrays into one single array and returns the array. The hstack is equivalent to concatenation.
Now to concatenate two arrays, In the sequence of arrays we will pass the given two arrays, The hstack() method will return the concatenated array.

Example

hstack( ([1 2 3 4 5] , [6 7 8 9 10]) ) ==> [ 1 2 3 4 5 6 7 8 9 10]

Syntax of hstack()

numpy.hstack(tuple_of_arrays)

Parameters:

tuple_of_arrays = Sequence of arrays to be passed to the function.

Return:

Returns The array formed by stacking the given arrays.    

Approach

  1. Import numpy library and create two numpy arrays
  2. Now pass the array1, array2 as a sequence of arrays to the hstack method
  3. The method will return return of a copy of the concatenated array.
  4. Print the concatenated array

Source code

import numpy as np

# create numpy arrays
arr1 = np.array([1, 2, 3, 4, 5])
arr2 = np.array([6, 7, 8, 9, 10])

#concatenating two arrays using hstack()
arr = np.hstack((arr1, arr2))

# printing the concatenated array
print(" The concatenated array is = ", arr)

OUTPUT:

The concatenated array is =  [ 1  2  3  4  5  6  7  8  9 10]

4.) Using vstack() to Concatenate NumPy Arrays row-wise (vertically)

Numpy module in python, provides a function numpy.vstack() function is used to Stack arrays in sequence vertically (row-wise). i.e, concatenating into a single array. The vstack method will take a sequence of arrays as parameters. It will stack the arrays into one single array and returns the array. The vstack is equivalent to vertical concatenation.

Now to Concatenate two arrays vertically (row-wise), we will pass the given two arrays to the vstack() method and it will return the concatenated (stacked) array.

Example

vstack( ([1 2 3 4 5] , [6 7 8 9 10]) )

Syntax of vstack()

numpy.vstack(tuple_of_arrays)

Parameters:

tuple_of_arrays = sequence of arrays to be passed to the function.

Return:

Returns The array formed by stacking the given arrays.    

Approach

  • Import numpy library and create two numpy arrays
  • Now pass the array1, array2 as a sequence of arrays to the vstack method
  • The method will return return of a copy of the concatenated array.
  • Print the concatenated array

Source code

import numpy as np

# create numpy arrays
arr1 = np.array([1, 2, 3, 4, 5])
arr2 = np.array([6, 7, 8, 9, 10])

#concatenating two arrays vertically (row-wise)
arr = np.vstack((arr1, arr2))

# printing the concatenated array
print(" The concatenated array is:")
print(arr)

OUTPUT:

The concatenated array is:

[[ 1  2  3  4  5]
 [ 6  7  8  9 10]]

Summary

Great! you made it, We have discussed all possible methods to concatenate a NumPy array to another NumPy array. Happy learning.

Pandas Tutorials -Learn Data Analysis with Python

   

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top