Logo

dev-resources.site

for different kinds of informations.

Array Manipulation: A Deep Dive into Insertions and Deletions

Published at
5/23/2024
Categories
python
programming
array
numpy
Author
lohith0512
Categories
4 categories in total
python
open
programming
open
array
open
numpy
open
Author
10 person written this
lohith0512
open
Array Manipulation: A Deep Dive into Insertions and Deletions

When you need to insert or remove one or more elements in an array, you can use the following functions to accomplish these tasks:


numpy.append is a function in the NumPy library, which is used to append values to the end of an array. It takes the array to which you want to append values, the values you want to append, and an axis parameter (optional) which specifies the axis along which the values are appended. If the axis parameter is not specified, the array is flattened before appending.

Here's the syntax:

numpy.append(array, values, axis=None)
Enter fullscreen mode Exit fullscreen mode
  • array: The array to which you want to append values.
  • values: The values you want to append to the array.
  • axis (optional): The axis along which the values are appended. If not provided, the array is flattened before appending.

Here's an example:

import numpy as np

# Create a numpy array
arr = np.array([1, 2, 3])

# Append a single value to the end of the array
arr = np.append(arr, 4)
print(arr)  

# Append multiple values to the end of the array
arr = np.append(arr, [5, 6, 7])
print(arr)  

# Append values along a specific axis
arr = np.array([[1, 2, 3], [4, 5, 6]])
print("Original array:")
print(arr)

# Append a row at the end of the array
arr = np.append(arr, [[7, 8, 9]], axis=0)
print("Appended along axis 0:")
print(arr)

# Append a column at the end of the array
arr = np.append(arr, [[10], [11], [12]], axis=1)
print("Appended along axis 1:")
print(arr)
Enter fullscreen mode Exit fullscreen mode
# Append a single value to the end of the array
 Output: [1 2 3 4]

# Append multiple values to the end of the array
 Output: [1 2 3 4 5 6 7]

# Append values along a specific axis
 Output:
 [[1 2 3]
  [4 5 6]]

# Append a row at the end of the array
 Output:
 [[1 2 3]
  [4 5 6]
  [7 8 9]]

# Append a column at the end of the array
 Output:
 [[ 1  2  3 10]
  [ 4  5  6 11]
  [ 7  8  9 12]]
Enter fullscreen mode Exit fullscreen mode

In this example, we first create a numpy array arr. Then, we use np.append() to append values to this array. We append single values, multiple values, and also append values along specific axes by specifying the axis parameter.


numpy.insert() is a function in the NumPy library used to insert elements into an array along a specified axis. It takes the following syntax:

numpy.insert(arr, obj, values, axis=None)
Enter fullscreen mode Exit fullscreen mode
  • arr: The input array.
  • obj: The index or indices before which values should be inserted. This can be an integer, a sequence of integers, or a slice object.
  • values: The scalar or array-like values to insert into arr.
  • axis: The axis along which to insert values. If not specified, arr is flattened before insertion.

Here's an example to illustrate how numpy.insert() works:

import numpy as np

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

# Insert a value at index 1 along axis 0 (rows)
new_arr = np.insert(arr, 1, [7, 8], axis=0)

print(new_arr)
Enter fullscreen mode Exit fullscreen mode

Output:

[[1 2]
 [7 8]
 [3 4]
 [5 6]]
Enter fullscreen mode Exit fullscreen mode

In this example, [7, 8] is inserted before the row at index 1 along axis 0. So, it becomes the second row in the resulting array.


numpy.delete is a function in the NumPy library for Python used to delete elements from an array along a specified axis. It returns a new array with the specified elements removed.

Here's the syntax:

numpy.delete(arr, obj, axis=None)
Enter fullscreen mode Exit fullscreen mode
  • arr: The input array.
  • obj: An index or a slice representing the elements to delete.
  • axis: The axis along which to delete the specified elements. If not provided, the input array is flattened before deletion.

Here's an example:

import numpy as np

# Create a sample array
arr = np.array([[1, 2, 3],
                [4, 5, 6],
                [7, 8, 9]])

# Delete the first row (index 0) along axis 0
new_arr = np.delete(arr, 0, axis=0)

print("Original array:")
print(arr)
print("Array after deleting the first row:")
print(new_arr)
Enter fullscreen mode Exit fullscreen mode

Output:

Original array:
[[1 2 3]
 [4 5 6]
 [7 8 9]]
Array after deleting the first row:
[[4 5 6]
 [7 8 9]]
Enter fullscreen mode Exit fullscreen mode

In this example, np.delete(arr, 0, axis=0) deletes the first row (index 0) along the vertical axis (axis=0) of the array arr, resulting in new_arr.

numpy Article's
30 articles in total
Favicon
Basics of Python in 1 minute
Favicon
Previewing a .npy file
Favicon
Python NumPy Tutorial for Beginners: Learn Array Creation, Indexing, and More
Favicon
A Visual Guide to Affine Transformations: Translation, Scaling, Rotation, and Shear
Favicon
NumPy for Machine Learning & Deep Learning
Favicon
Investigating the performance of np.einsum
Favicon
The Unreasonable Usefulness of numpy's einsum
Favicon
ML Zoomcamp Week 1
Favicon
Python Data Wrangling and Data Quality
Favicon
Build Your Own AI Language Model with Python and NumPy
Favicon
Streamline Your NumPy File Conversions with npyConverter
Favicon
Streamline Plots: NumPy to Jupyter, No Loops
Favicon
PYTHON 101: INTRODUCTION TO PYTHON FOR DATA ANALYTICS
Favicon
How to Install NumPy in PyCharm on a Mac
Favicon
NumPy for the Curious Beginner
Favicon
NumPy: The Superhero Library Python Deserves (But Maybe Didn't Know It Needed)
Favicon
Transforming Simplicity: Adapting Linear Regression to Capture Complex Non-Linear Phenomena with NumPy
Favicon
A Beginner's Guide to Python Libraries
Favicon
fix: A module that was compiled using NumPy 1.x cannot be run in NumPy 2.0.0 as it may crash
Favicon
NumPy Asarray Function: A Comprehensive Guide
Favicon
Device conversion with to() and from_numpy() and numpy() in PyTorch
Favicon
Master Linear Regression with NumPy: Step-by-Step Guide to Building and Optimizing Your First Model!
Favicon
A Comprehensive Guide to NumPy with Python 🐍🎲
Favicon
Creating Line Plots with Object-Oriented API and Subplot Function in Python
Favicon
Numpy Isnumeric Function: Mastering Numeric String Validation
Favicon
Element-Wise Numerical Operations in NumPy: A Practical Guide with Examples
Favicon
NumPy for Beginners: Why You Should Rely on Numpy Arrays More
Favicon
NumPy's Argmax? How it Finds Max Elements from Arrays
Favicon
5 Exciting NumPy Challenges to Boost Your Programming Skills! 🚀
Favicon
Array Manipulation: A Deep Dive into Insertions and Deletions

Featured ones: