Append Numpy Array

Append Numpy Array

One of the common operations you may need to perform with arrays is appending new elements to an existing array.Numpy is one of the most popular libraries used for numerical computing in Python. It provides a powerful array object called np.array that allows you to efficiently store and manipulate large multidimensional arrays of homogeneous data.

In this article, we will explore various methods to append arrays in Numpy, along with code examples and their execution results. We will cover both 1-dimensional and multi-dimensional cases.

Introduce append numpy array

The numpy.append() function is a versatile method that allows you to append elements to an existing array along a specified axis. It takes three parameters – the array to which you want to append elements, the values to append, and the axis along which the values should be appended. Let’s explore this with some code examples:

Example 1:

import numpy as np

arr = np.array([1, 2, 3])
values = [4, 5, 6]

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

Output:

Append Numpy Array

Explanation: In this example, we have an existing 1-dimensional array arr with values [1, 2, 3]. We want to append another 1-dimensional array values with values [4, 5, 6] to arr. The np.append() function concatenates the two arrays and returns a new array with the appended values.

Example 2:

import numpy as np

arr = np.array([[1, 2, 3], [4, 5, 6]])
values = [[7, 8, 9], [10, 11, 12]]

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

Output:

Append Numpy Array

Explanation: In this example, we have a 2-dimensional array arr with shape (2, 3). We want to append another 2-dimensional array values with the same shape (2, 3) to arr. By specifying axis=0, we are appending the values along the rows. The resulting array has shape (4, 3).

Introduce append a single value

We can append a single value to a numpy array using numpy.append(). Let’s see an example:

Example 3:

import numpy as np

arr = np.array([1, 2, 3])
new_arr = np.append(arr, 4)

print(new_arr)

Output:

Append Numpy Array

In the above example, we append a single value 4 to the numpy array arr. The resulting array new_arr contains the original elements from arr along with the appended value.

Introduce append multiple values

To append multiple values to a numpy array, we can pass a list of values as the second parameter of numpy.append().

Example 4:

import numpy as np

arr = np.array([1, 2, 3])
new_arr = np.append(arr, [4, 5, 6])

print(new_arr)

Output:

Append Numpy Array

In this example, we use numpy.append() to append the values [4, 5, 6] to the numpy array arr. The resulting array new_arr contains the original elements from arr followed by the appended values.

Introduce append arrays with different dimensions

Numpy allows us to append arrays with different dimensions along a specified axis.

Example 5:

import numpy as np

arr1 = np.array([[1, 2, 3]])
arr2 = np.array([[4, 5, 6]])
new_arr = np.append(arr1, arr2, axis=0)

print(new_arr)

Output:

Append Numpy Array

In this example, we define two arrays arr1 and arr2 with shapes (1, 3). By using numpy.append() with axis=0, we append arr2 to the end of arr1 along the first axis. The resulting array new_arr has shape (2, 3) and contains the values from both arr1 and arr2.

Introduce append arrays with different dimensions along axis 1

We can also append arrays with different dimensions along axis 1.

Example 6:

import numpy as np

arr1 = np.array([[1], [2], [3]])
arr2 = np.array([[4], [5], [6]])
new_arr = np.append(arr1, arr2, axis=1)

print(new_arr)

Output:

Append Numpy Array

In this example, we define arr1 and arr2 as column vectors. By specifying axis=1 in numpy.append(), we append arr2 to the right side of arr1, resulting in the array new_arr with shape (3, 2).

Introduce append along different axes

In numpy, we can append arrays along different axes simultaneously.

Example 7:

import numpy as np

arr = np.array([1, 2, 3])
arr1 = np.array([4, 5, 6])
arr2 = np.array([7, 8, 9])

new_arr = np.append(arr, [arr1, arr2])

print(new_arr)

Output:

Append Numpy Array

In this example, we define three arrays: arr, arr1, and arr2. By using numpy.append() with axis=0, we append arr1 and arr2 as separate arrays to the arr. The resulting array new_arr contains all the elements along the specified axis.

Introduce append arrays with different data types

Numpy arrays can handle different data types. Let’s consider an example where we append arrays with different data types.

Example 8:

import numpy as np

arr1 = np.array([1, 2, 3])
arr2 = np.array(['welcome', 'to', 'https://numpywhere.com/'])

new_arr = np.append(arr1, arr2)

print(new_arr)

Output:

Append Numpy Array

In this example, we define two arrays: arr1 with integers and arr2 with strings. By using numpy.append(), we append arr2 at the end of arr1. The resulting array new_arr contains all the elements, converting the numbers to strings.

Introduce numpy hstack

The numpy.hstack() function is used to horizontally stack arrays. It takes a sequence of arrays as input and concatenates them horizontally. Let’s see how it works with an example:

Example 9:

import numpy as np

arr1 = np.array([0, 1, 2])
arr2 = np.array([3, 4, 5])

result = np.hstack((arr1, arr2))
print(result)

Output:

Append Numpy Array

Explanation: In this example, we have two 1-dimensional arrays arr1 and arr2. The np.hstack() function horizontally stacks these arrays by concatenating them. The resulting array contains all the elements from both arrays in a single 1-dimensional array.

Introduce numpy vstack

The numpy.vstack() function is used to vertically stack arrays. It takes a sequence of arrays as input and concatenates them vertically. Let’s see an example:

Example 10:

import numpy as np

arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])

result = np.vstack((arr1, arr2))
print(result)

Output:

Append Numpy Array

Explanation: In this example, we have two 1-dimensional arrays arr1 and arr2. The np.vstack() function vertically stacks these arrays by concatenating them. The resulting array contains both arrays as rows in a 2-dimensional array.

Introduce numpy concatenate

The numpy.concatenate() function can be used to concatenate arrays along an existing axis. It takes a sequence of arrays and the axis along which the arrays should be concatenated. Let’s see an example:

Example 11:

import numpy as np

arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])

result = np.concatenate((arr1, arr2))
print(result)

Output:

Append Numpy Array

Explanation: In this example, we have two 1-dimensional arrays arr1 and arr2. The np.concatenate() function concatenates these arrays by merging them into a single 1-dimensional array.

Introduce numpy column_stack

The numpy.column_stack() function is used to stack 1-dimensional arrays as columns into a 2-dimensional array. Let’s see how it works:

Example 12:

import numpy as np

arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])

result = np.column_stack((arr1, arr2))
print(result)

Output:

Append Numpy Array

Explanation: In this example, we have two 1-dimensional arrays arr1 and arr2. The np.column_stack() function stacks these arrays as columns into a 2-dimensional array. Each array becomes a column in the resulting array.

Introduce numpy row_stack

The numpy.row_stack() function is used to stack 1-dimensional arrays as rows into a 2-dimensional array. Let’s see an example:

Example 13:

import numpy as np

arr1 = np.array([0, 1, 2])
arr2 = np.array([3, 4, 5])

result = np.row_stack((arr1, arr2))
print(result)

Output:

Append Numpy Array

Explanation: In this example, we have two 1-dimensional arrays arr1 and arr2. The np.row_stack() function stacks these arrays as rows into a 2-dimensional array. Each array becomes a row in the resulting array.

Introduce numpy insert

The numpy.insert() function allows you to insert elements into an array along a specified axis and at a particular position. Let’s explore this with an example:

Example 14:

import numpy as np

arr = np.array([1, 2, 3, 4, 5])

result = np.insert(arr, 2, [100, 200])
print(result)

Output:

Append Numpy Array

Explanation: In this example, we have an existing 1-dimensional array arr with values [1, 2, 3, 4, 5]. We want to insert the values [100, 200] at index 2. The np.insert() function inserts the values at the specified position and returns a new array.

Introduce numpy resize

The numpy.resize() function is used to resize an array to a specified size. If the new size is larger than the original size, the array is filled with repeated copies of the original data. If the new size is smaller, the original data is truncated. Let’s see an example:

Example 15:

import numpy as np

arr = np.array([1, 2, 3, 4, 5])

result = np.resize(arr, (3, 3))
print(result)

Output:

Append Numpy Array

Explanation: In this example, we have an existing 1-dimensional array arr with values [1, 2, 3, 4, 5]. We want to resize the array to a shape of (3, 3). The np.resize() function resizes the array and repeats the original data to fit the new size.

Compare numpy append with concatenate

Example 16:

import numpy as np

arr1 = np.array([[1, 2, 3], [4, 5, 6]])
arr2 = np.array([[7, 8, 9]])

result1 = np.append(arr1, arr2, axis=0)
result2 = np.concatenate((arr1, arr2), axis=0)

print(result1)
print(result2)

Output:

Append Numpy Array

Explanation: In this example, we have a 2-dimensional array arr1 with shape (2, 3) and another 2-dimensional array arr2 with shape (1, 3). We want to append arr2 to arr1 along the rows. The np.append() function and np.concatenate() function produce the same result, which is a new array with shape (3, 3), containing all the rows from both arrays.

Conclusion of append numpy array

In this article, we have explored various methods to append Numpy arrays. We started by using the numpy.append() function, which allows you to append elements along a specified axis. Then, we looked at functions like numpy.hstack(), numpy.vstack(), and numpy.concatenate() that provide different ways to stack arrays horizontally and vertically.

We also discussed functions like numpy.column_stack() and numpy.row_stack() that stack 1-dimensional arrays into 2-dimensional arrays as columns and rows respectively. Additionally, we covered the numpy.insert() function to insert elements at a specific position in an array and the numpy.resize() function to resize arrays.

By understanding these methods, you can efficiently manipulate arrays in Numpy and perform operations like appending, stacking, and inserting elements based on your requirements.

Like(1)