Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

NumPy - Append Values to an Array



Append Values to an Arrays in NumPy

Appending values to an array in NumPy means adding new elements or arrays to an existing NumPy array. This operation involves creating a new array that includes the original elements along with the new ones, as NumPy arrays have fixed sizes and do not support in-place modifications like traditional lists.

Appending Values to a 1D Array

Appending values to a 1D array in NumPy involves adding new elements to the end of an existing one-dimensional array. Since NumPy arrays have a fixed size, this operation creates a new array with the original elements plus the newly appended values. To achieve this, we can use the np.append() function in NumPy. Following is the syntax −

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

Where,

  • arr: The original array to which values will be appended.
  • values: The values to be appended. Can be a single value or an array.
  • axis: The axis along which values are appended. For 1D arrays, this parameter is ignored and can be None.

Example

In the following example, we are using the np.append() to add elements to a 1D array: first appending a single value, and then multiple values −

import numpy as np

# Create an initial 1D array
arr = np.array([1, 2, 3])

# Append a single value
arr_appended_single = np.append(arr, 4)

# Append multiple values
arr_appended_multiple = np.append(arr, [4, 5, 6])

print("Array after appending a single value:", arr_appended_single)
print("Array after appending multiple values:", arr_appended_multiple)

Following is the output obtained −

Array after appending a single value: [1 2 3 4]
Array after appending multiple values: [1 2 3 4 5 6]

Appending Values to a 2D Array

Appending values to a 2D array in NumPy involves adding rows or columns to an existing two-dimensional array. Since NumPy arrays have a fixed size, appending values results in creating a new array that combines the original data with the new data. Let us explore various ways and methods used to append values to a 2D array −

Appending 2D Rows

To append rows to a 2D array, we can use the np.vstack() function. This function stacks arrays vertically or concatenate along axis 0. Following is the syntax −

numpy.vstack(tup)

Where, tup is a sequence of arrays to be stacked vertically. All arrays must have the same number of columns.

Example

In this example, we are using the np.vstack() function to append rows to a 2D array, adding new rows beneath the existing ones −

import numpy as np

# Create an initial 2D array
arr = np.array([[1, 2], [3, 4]])

# Create an array of rows to append
rows_to_append = np.array([[5, 6], [7, 8]])

# Append rows to the initial array
arr_appended_rows = np.vstack((arr, rows_to_append))

print("Array after appending rows:")
print(arr_appended_rows)

The result is a new array with the additional rows stacked at the bottom −

Array after appending rows:
[[1 2]
 [3 4]
 [5 6]
 [7 8]]

Appending 2D Columns

To append columns to a 2D array, we can use the np.hstack() function. This function stacks arrays horizontally or concatenate along axis 1. Following is the syntax −

numpy.hstack(tup)

Where, tup is a sequence of arrays to be stacked horizontally. All arrays must have the same number of rows.

Example

In the example below, we are using the np.hstack() function to append columns to a 2D array, adding new columns to the right of the existing ones −

import numpy as np

# Create an initial 2D array
arr = np.array([[1, 2], [3, 4]])

# Create an array of columns to append
columns_to_append = np.array([[5], [6]])

# Append columns to the initial array
arr_appended_columns = np.hstack((arr, columns_to_append))

print("Array after appending columns:")
print(arr_appended_columns)

The result is a new array with the additional columns placed alongside the original data −

Array after appending columns:
[[1 2 5]
 [3 4 6]]

Appending to Multi-dimensional Arrays

Appending to multi-dimensional arrays in NumPy involves adding new elements along specified axes. Unlike 1D and 2D arrays, multi-dimensional arrays (e.g., 3D or higher) require careful alignment of the dimensions and axes along which you want to append data.

Example

In the following example, we are using the np.append() function to add values to a 3D array along the first axis −

import numpy as np

# Original 3D array
arr = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])

# Values to append (must match dimensions)
values = np.array([[[9, 10], [11, 12]], [[13, 14], [15, 16]]])

# Append values along axis 0
result = np.append(arr, values, axis=0)
print(result)

The resulting array has the new values appended as additional layers on top of the original array −

[[[ 1  2]
 [ 3  4]]

[[ 5  6]
 [ 7  8]]

[[ 9 10]
 [11 12]]

[[13 14]
 [15 16]]]

Appending with Different Data Types

Appending arrays with different data types in NumPy requires careful handling because NumPy arrays are homogeneous; they must contain elements of the same data type.

When appending arrays with different data types, NumPy will generally perform type coercion or create a new array with a common data type that can contains all the appended data.

Example

In this example, we are using the np.append() to add floating-point values to an integer array −

import numpy as np

# Original array of integers
arr = np.array([1, 2, 3])

# Values to append (floating-point)
values = np.array([4.5, 5.5])

# Append values
result = np.append(arr, values)
print(result)  

After executing the above code, we get the following output −

[1.  2.  3.  4.5 5.5]

Append Using np.concatenate() Function

We can use the np.concatenate() function for combining arrays along a specified axis. It is particularly useful for appending data to existing arrays. Following is the syntax −

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

Where,

  • a1, a2, ...: Arrays to be concatenated. They must have the same shape except in the dimension corresponding to the axis parameter.
  • axis: The axis along which to concatenate the arrays. 0 is the default for 1D arrays, and other values for multi-dimensional arrays.

Example

In the following example, we are concatenating two 2D arrays along axis "0" using the np.concatenate() function −

import numpy as np

# Arrays to concatenate
arr1 = np.array([[1, 2], [3, 4]])
arr2 = np.array([[5, 6], [7, 8]])

# Concatenate along axis 0
result = np.concatenate((arr1, arr2), axis=0)
print(result)

The result produced is as follows −

[[1 2]
 [3 4]
 [5 6]
 [7 8]]
Advertisements