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

#### Table Of Contents

- Using append() method to concatenate NumPy Arrays
- Using concatenate() method to concatenate NumPy Arrays
- Using hstack() method to Concatenate NumPy Arrays
- Using vstack() to Concatenate NumPy Arrays row-wise (vertically)

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.

#### Approach

- Import numpy library and create two numpy arrays
- Now pass the array1, array2 to the append() 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 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.

### Frequently Asked:

- Python: Convert Matrix / 2D Numpy Array to a 1D Numpy Array
- Check if any value in Numpy Array is negative in Python
- How to remove multiple elements from a NumPy array?
- How to generate Random Array in NumPy?

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

- Import numpy library and create two numpy arrays
- Now pass the array1, array2 as a sequence of arrays to the hstack 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 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.