| by Arround The Web | No comments

Numpy Array Append

Arrays” are collections of values that can have one or more dimensions. For example, a one-dimensional array can represent a vector, a two-dimensional array can represent a matrix, and so on. One of the common operations that you may need to perform on arrays is appending. “Appending” means adding new elements to the end/last of an existing array. This can be useful for updating your data dynamically, concatenating multiple arrays, or inserting values at specific positions.

This Python article will explore how to append arrays in “NumPy” using different methods and functions.

How to Append Python NumPy Arrays?

To append the Python NumPy arrays, the below approaches can be used:

Method 1: Append Python NumPy Arrays Using the “numpy.append()” Method

The “numpy.append()” method is utilized to append values to the end of the defined array.

Syntax

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

As shown in the above syntax, “arr” refers to the defined array, “values” are the values to be added, and “axis” indicates the axis along which the values will be appended.

Example 1: Appending an Array With the Specified Array

The below example code is used to append an array with another array:

import numpy
array1 = numpy.array([10,12,14])
print('Original Array: ', array1)
print('\nAfter Appending New Array: ', numpy.append(array1, [16,18,20]) )

In the above code:

  • The “numpy” library is imported and the “array()” method is used to create an array, respectively.
  • The “append()” method takes the defined array as its first argument and another array as its second argument and returns a new array after appending the defined array with a newly initialized array.

Output

As seen, the input array has been appended accordingly.

Example 2: Appending an Array With a Specified Array in Rows

The following code is used to append an array with a specified array in rows via “axis = 0”:

import numpy
array1 = numpy.array([[10,12,14],[16,18,20]])
print('Original Array:\n', array1)
print('\nAfter Appending New Array:\n', numpy.append(array1, [[25,27,29],[33,35,37]],axis = 0))

In the above code:

  • The “numpy” library is imported and the “array()” function creates a “2-D” array with two rows and three columns.
  • The “append()” method is used to append another two-dimensional array.
  • The “axis = 0” argument tells the function to append the new array along the rows “axis = 0” of the defined array so that the final array has four rows and three columns.

Output

The array has been appended in rows successfully.

Example 3: Appending an Array With a Specified Array in Columns

The below code appends an array in columns with “axis=1”:

import numpy
array1 = numpy.array([[10,12,14],[16,18,20]])
print('Original Array:\n', array1)
print('\nAfter Appending New Array:\n', numpy.append(array1, [[25,27,29],[33,35,37]],axis = 1))

In the above code block, the “numpy.append()” method is used to append the specified array to the initialized array along the second axis “axis = 1”, which means adding columns.

Output

The array has been appended along the “1” axis i.e., “columns” appropriately.

Method 2: Append Python NumPy Arrays Using “numpy.concatenate()” Function

The “numpy.concatenate()” function combines “Numpy” arrays together. It is utilized for joining multiple arrays of the same shape along a particular axis.

Syntax

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

In the above syntax:

  • The “(arr1, arr2, …)” parameters correspond to the sequence of the arrays to be joined. The only difference between them is the dimension corresponding to the “axis“.
  • The “axis” parameter is the direction in which arrays are joined and if the “axis” is “None”, arrays are flattened before being utilized and the “axis” value is assigned/set to “0” by default.
  • The “out”, “dtype” or “casting” is an optional argument that specifies the destination array to place the result, the data type of the destination array, or controls what kind of data casting may occur, respectively.

Example

The below code is used to append a Python Numpy array:

import numpy
array1 = numpy.array([10,12,14])
print('Array 1:', array1)
array2 = numpy.array([16,18,20])
print('Array 2:', array2)
print('After Appending Array:', numpy.concatenate((array1, array2)))

In the above code, the “numpy.concatenate()” function takes both the arrays as its arguments and appends them to each other.

Output

Based on the above output, the array has been appended.

Conclusion

The “numpy.append()” method and the “numpy.concatenate()” method of the “numpy” library are used to append the NumPy array in Python. Both methods effectively append the specified array to the existing one. The “numpy.append()” method, however, takes an optional “axis” argument to append the arrays along rows and columns. This blog discussed the concept of appending a NumPy array in Python.

Share Button

Source: linuxhint.com

Leave a Reply