NumPy Append

numpy append

In this article we will take a deep dive into NumPy Append function, we will explore their usage, benefits, and best practices, along with examples.


What does NumPy Append function do?

Appending in NumPy involves adding elements or arrays to the end of an existing array. It enables users to extend array dimensions or incorporate new data at the end seamlessly.

np.append

  • np.append():
    • The np.append() function appends values to the end of an array along a specified axis.
    • It enables both single-value and array append, providing flexibility in data augmentation.
  • Syntax
    • np.append(arr, values, axis=None)
      • arr: The array to which values will be appended.
      • values: The values to be appended to arr. This can be a single value, an array-like object, or a sequence of arrays.
      • axis: The axis along which the values will be appended. If not specified, the array is flattened before appending.

Benefits and Applications of Append

  • Efficient Data Expansion:
    • Append facilitates seamless addition of new elements or arrays to existing arrays, extending dimensions dynamically.
    • It simplifies the process of appending new data to arrays, ensuring compatibility and consistency.

Best Practices for Append

  • Axis Specification:
    • Specify the axis along which append should occur to ensure the desired outcome.
    • Understand the dimensions of your arrays and how they align along the append axis.

Append in 1D array

Singel Value
arr = np.array([1,2,3,4,5])
new_arr = np.append(arr,20)
print(new_arr)
  • We have an existing 1D NumPy array arr containing the elements [1, 2, 3, 4, 5].
  • We use the np.append() function to append the value 20 to the end of the array arr.
[ 1  2  3  4  5 20]
  • This output shows the modified array new_arr after the append operation. The value 20 has been appended to the end of the original array arr.
Multiple Value
arr = np.array([1, 2, 3, 4, 5])
new_values = np.array([10, 20])

appended_arr = np.append(arr, new_values)
print(appended_arr)

Append in 2D array

axis = 0
arr = np.array([[1, 2, 3], [4, 5, 6]])
new_row = np.array([7, 8, 9])

appended_arr = np.append(arr, [new_row], axis=0)
print(appended_arr)
  • We have a 2D array arr with two rows and three columns.
  • We create a new row new_row to append to the existing array.
  • Using np.append(), we append new_row to arr along axis 0 (rows).
[[1 2 3]
[4 5 6]
[7 8 9]]
  • The resulting appended_arr contains the original array arr with the new row appended at the end.
axis = 1
arr = np.array([[1, 2, 3], [4, 5, 6]])
new_column = np.array([[7], [8]])

appended_arr = np.append(arr, new_column, axis=1)
print(appended_arr)
  • We have a 2D array arr with two rows and three columns.
  • We create a new column new_column to append to the existing array.
  • Using np.append(), we append new_column to arr along axis 1 (columns).
[[1 2 3 7]
[4 5 6 8]]
  • The resulting appended_arr contains the original array arr with the new column appended at the end.

In conclusion, the numpy.append() function stands as a versatile tool for expanding and modifying NumPy arrays by adding new elements or arrays along specified axes. Its simplicity and efficiency make it invaluable for various data manipulation tasks in scientific computing, machine learning, and beyond. By familiarizing oneself with its syntax and functionality, users can seamlessly integrate array expansion and modification into their data processing workflows. As you explore the myriad applications of numpy.append(), consider sharing your insights and experiences with fellow data enthusiasts.

Sharing educational articles or tutorials on platforms such as blogs, forums, or social media can contribute to the collective knowledge and foster collaboration within the data science community. Together, we can continue to unlock new possibilities and push the boundaries of data analysis and manipulation using NumPy. Happy learning guys.


We hope that you liked our information, if you liked our information, then you must share it with your friends, family and group. So that they can also get this information.

Reference: np.append()

Also Read:

Spread the love

Leave a Comment