In this article we will learn about NumPy Insert, we will explore their usage, benefits, and best practices, along with examples.
What does NumPy Insert function do?
Insertion in NumPy involves adding elements or arrays into specified positions within an existing array. It allows users to expand array dimensions or incorporate new data seamlessly.
NumPy Insert
np.insert()
:- The
np.insert()
function inserts values along a specified axis at given indices within an array. - It enables both single-value and array insertion, providing flexibility in data augmentation.
- The
- Syntax
- numpy.insert(arr, obj, values, axis=None)
- arr: This is the input array where the insertion operation will take place.
- obj: This specifies the index or indices before which values need to be inserted. It can be:
- An integer: If obj is an integer, values are inserted before the objth element along the specified axis.
- A slice object: If obj is a slice object, values are inserted before the elements indicated by the slice along the specified axis.
- An array-like: If obj is an array-like object, values are inserted before the elements indicated by the array-like object along the specified axis.
- values: These are the values to be inserted into the array. It can be a single value or an array of values. If values is a single value, it is broadcasted to fit the shape of the insertion.
- axis: This is the axis along which the insertion should be performed. By default, axis is None, and the input array is flattened before the insertion. If axis is an integer, values are inserted along the specified axis. If axis is None, values are appended to the flattened input array.
- numpy.insert(arr, obj, values, axis=None)
Benefits and Applications of Insertion
- Dynamic Data Management:
- Insertion facilitates dynamic expansion of array dimensions, accommodating additional data seamlessly.
- It enables users to incorporate new elements or arrays into existing arrays, adapting to changing requirements.
Best Practices for Insertion
- Indexing Accuracy:
- Ensure accurate indexing when performing insertion to avoid unintended data corruption.
- Double-check indices and axes to prevent errors and maintain data integrity.
Practical Example for Insertion
Single value insertion
arr = np.array([1, 2, 3, 4, 5])
new_arr = np.insert(arr,1,45)
print(new_arr)
np.insert(arr, 1, 45)
: This line uses thenp.insert()
function to insert the value45
before the element at index1
in the arrayarr
.
[ 1 45 2 3 4 5]
- The value
45
is inserted before the element at index1
(which is2
) in the original arrayarr
. - The resulting array
new_arr
contains the original elements ofarr
with the value45
inserted at the specified index.
Multiple Value Insertion
arr = np.array([1, 2, 3, 4, 5])
new_values = np.array([10, 20])
inserted_arr = np.insert(arr, 2, new_values)
print(inserted_arr)
- We have an existing NumPy array
arr
containing the elements[1, 2, 3, 4, 5]
. - We create a new NumPy array
new_values
containing the elements[10, 20]
. - The
np.insert()
function inserts the values fromnew_values
before the 2nd index of the arrayarr
.
[ 1 2 10 20 3 4 5]
- The values
[10, 20]
are inserted between the elements2
and3
. - The resulting array becomes
[1, 2, 10, 20, 3, 4, 5]
, where[10, 20]
are inserted before the element3
at the 3rd index of the original array.
Insertion in 2D array
axis = 0
arr = np.array([[1, 2, 3], [4, 5,6],[7,8,9]])
new_arr = np.insert(arr, 1, [120, 140, 180], axis = 0)
print(new_arr)
- We use the
np.insert()
function to insert a new row[120, 140, 180]
before the row at index1
along axis0
(rows).
[[ 1 2 3]
[120 140 180]
[ 4 5 6]
[ 7 8 9]]
- This output shows the modified array
new_arr
after the insertion operation. The new row[120, 140, 180]
has been inserted before the row[4, 5, 6]
at index1
.
axis = 1
arr = np.array([[1, 2, 3], [4, 5,6],[7,8,9]])
new_arr = np.insert(arr, 1, [120, 140, 180], axis = 1)
print(new_arr)
- We use the
np.insert()
function to insert a new column[120, 140, 180]
before the column at index1
along axis1
(columns).
[[ 1 120 2 3]
[ 4 140 5 6]
[ 7 180 8 9]]
- This output shows the modified array
new_arr
after the insertion operation. The new column[120, 140, 180]
has been inserted before the column[2, 5, 8]
at index1
.
Reference: np.insert()
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.
Also Read: