Open In App

How to append two NumPy Arrays?

Last Updated : 09 Aug, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisites: Numpy

Two arrays in python can be appended in multiple ways and all possible ones are discussed below.

Method 1: Using append() method

This method is used to Append values to the end of an array.

Syntax :

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

Parameters :

  • array: [array_like]Input array.
  • values : [array_like]values to be added in the arr. Values should be 
    shaped so that arr[…,obj,…] = values. If the axis is defined values can be of any 
    shape as it will be flattened before use. 
     
  • axis : Axis along which we want to insert the values. By default, array is flattened.

Return :

A copy of array with values being appended at the end as per the mentioned object, along a given axis.

Example:

Python3




import numpy
 
 
array1 = numpy.array([1, 2, 3, 4, 5])
array2 = numpy.array([6, 7, 8, 9, 10])
 
# Appending both arrays using Append method
array1 = numpy.append(array1, array2)
print(array1)


 Output:

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

Method 2: Using concatenate() method

Concatenate method Joins a sequence of arrays along an existing axis.

Syntax : 

numpy.concatenate((arr1, arr2, …), axis=0, out=None)
 

Parameters :

  • arr1, arr2, … : [sequence of array_like] The arrays must have the same shape, except in the dimension corresponding to axis.
  • axis : [int, optional] The axis along which the arrays will be joined. If axis is None, arrays are flattened before use. Default is 0.
  • out : [ndarray, optional] If provided, the destination to place the result. The shape must be correct, matching that of what concatenate would have returned if no out argument were specified.

Return : [ndarray] The concatenated array.

Example:

Python3




import numpy
 
 
array1 = numpy.array([1, 2, 3, 4, 5])
array2 = numpy.array([6, 7, 8, 9, 10])
 
# Appending both Arrays using concatenate() method.
array1 = numpy.concatenate([array1, array2])
print(array1)


Output:

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

Method 3: Using stack() method

Stack method Joins a sequence of arrays along a new axis.

Syntax : numpy.stack(arrays, axis)

Parameters :
 

  • arrays : [array_like] Sequence of arrays of the same shape.
  • axis : [int] Axis in the resultant array along which the input arrays are stacked.

Return : [stacked ndarray] The stacked array of the input arrays which has one more dimension than the input arrays.

Example:

Python3




import numpy
 
 
array1 = numpy.array([1, 2, 3, 4, 5])
array2 = numpy.array([6, 7, 8, 9, 10])
 
# Join a sequence of arrays along a new axis.
array1 = numpy.stack([array1, array2])
print(array1)


Output:

[[ 1  2  3  4  5] 
 

 [ 6  7  8  9 10]]

Method 4: Using hstack() method

hstack method Stacks arrays in sequence horizontally (column wise).

Syntax : numpy.hstack(tup)

Parameters :
 

  • tup : [sequence of ndarrays] Tuple containing arrays to be stacked. The arrays must have the same shape along all but the second axis.

Return : [stacked ndarray] The stacked array of the input arrays.

Example:

Python3




import numpy
 
 
array1 = numpy.array([1, 2, 3, 4, 5])
array2 = numpy.array([6, 7, 8, 9, 10])
 
# Stack arrays in sequence horizontally (column wise).
array1 = numpy.hstack([array1, array2])
print(array1)


Output:

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

Method 5: Using vstack() method

vstack method Stacks arrays in sequence vertically (row wise).

Syntax : numpy.vstack(tup)

Parameters :
 

  • tup : [sequence of ndarrays] Tuple containing arrays to be stacked. The arrays must have the same shape along all but the first axis.

Return : [stacked ndarray] The stacked array of the input arrays.

Example:

Python3




import numpy
 
 
array1 = numpy.array([1, 2, 3, 4, 5])
array2 = numpy.array([6, 7, 8, 9, 10])
 
# Stack arrays in sequence vertically (row wise).
array1 = numpy.vstack([array1, array2])
print(array1)


Output:

[[ 1  2  3  4  5] 
 

 [ 6  7  8  9 10]]

Method 6: Using column_stack() method

The column_stack() method Stacks arrays as columns into a 2-D array.

Syntax: column_stack( array)

Python3




import numpy
 
 
array1 = numpy.array([1, 2, 3, 4, 5])
array2 = numpy.array([6, 7, 8, 9, 10])
 
# Stack 1-D arrays as columns into a 2-D array.
array1 = numpy.column_stack([array1, array2])
print(array1)


Output:

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


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads