How to append two NumPy Arrays?
Last Updated :
09 Aug, 2021
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]]
Similar Reads
How to Swap Two Rows in a NumPy Array One common task you might encounter when working with NumPy arrays is the need to swap two rows. Swapping rows can be essential in data preprocessing, reshaping data, or reordering data to perform specific analyses in Python. In this article, we will explore different methods to swap two rows in a N
4 min read
How to append a NumPy array to an empty array in Python In this article, we will cover how to append a NumPy array to an empty array in Python. Â Here, we will discuss 2 different methods to append into an empty NumPy array. Both of these methods differ slightly, as shown below: Append a NumPy array to an empty array using the appendExample 1 Here we are
2 min read
How to save a NumPy array to a text file? When working with data it's important to know how to save NumPy arrays to text files for storage, sharing and further analysis. There are different ways from manual file handling to using specialized NumPy functions. In this article, we will see how to save a NumPy array to a text fileMethod 1: Usin
3 min read
How To Save Multiple Numpy Arrays NumPy is a powerful Python framework for numerical computing that supports massive, multi-dimensional arrays and matrices and offers a number of mathematical functions for modifying the arrays. It is an essential store for Python activities involving scientific computing, data analysis, and machine
3 min read
How to convert a list and tuple into NumPy arrays? In this article, let's discuss how to convert a list and tuple into arrays using NumPy. NumPy provides various methods to do the same using Python. Example: Input: [3, 4, 5, 6]Output: [3 4 5 6]Explanation: Python list is converted into NumPy ArrayInput: ([8, 4, 6], [1, 2, 3])Output: [[8 4 6] [1 2 3]
2 min read
numpy.append() in Python numpy.append() function is used to add new values at end of existing NumPy array. This is useful when we have to add more elements or rows in existing numpy array. It can also combine two arrays into a bigger one. Syntax: numpy.append(array, values, axis = None)array: Input array. values: The values
2 min read
numpy.ndarray.view() in Python numpy.ndarray.view() helps to get a new view of array with the same data. Syntax: ndarray.view(dtype=None, type=None)Parameters: dtype : Data-type descriptor of the returned view, e.g., float32 or int16. The default, None, results in the view having the same data-type as a. type : Python type, opti
3 min read
Convert Python List to numpy Arrays NumPy arrays are more efficient than Python lists, especially for numerical operations on large datasets. NumPy provides two methods for converting a list into an array using numpy.array() and numpy.asarray(). In this article, we'll explore these two methods with examples for converting a list into
4 min read
Appending values at the end of an NumPy array Let us see how to append values at the end of a NumPy array. Adding values at the end of the array is a necessary task especially when the data is not fixed and is prone to change. For this task, we can use numpy.append() and numpy.concatenate(). This function can help us to append a single value as
4 min read
How to Concatenate two 2-dimensional NumPy Arrays? Sometimes it might be useful or required to concatenate or merge two or more of these NumPy arrays. In this article, we will discuss various methods of concatenating two 2D arrays. But first, we have to import the NumPy package to use it: # import numpy package import numpy as np Then two 2D arrays
4 min read