In this article we will learn about NumPy Insert, we will explore their usage, benefits, and best practices, along with examples.
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()
: np.insert()
function inserts values along a specified axis at given indices within an array.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 the np.insert()
function to insert the value 45
before the element at index 1
in the array arr
.[ 1 45 2 3 4 5]
45
is inserted before the element at index 1
(which is 2
) in the original array arr
.new_arr
contains the original elements of arr
with the value 45
inserted at the specified index.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)
arr
containing the elements [1, 2, 3, 4, 5]
.new_values
containing the elements [10, 20]
.np.insert()
function inserts the values from new_values
before the 2nd index of the array arr
.[ 1 2 10 20 3 4 5]
[10, 20]
are inserted between the elements 2
and 3
.[1, 2, 10, 20, 3, 4, 5]
, where [10, 20]
are inserted before the element 3
at the 3rd index of the original array.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)
np.insert()
function to insert a new row [120, 140, 180]
before the row at index 1
along axis 0
(rows).[[ 1 2 3]
[120 140 180]
[ 4 5 6]
[ 7 8 9]]
new_arr
after the insertion operation. The new row [120, 140, 180]
has been inserted before the row [4, 5, 6]
at index 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)
np.insert()
function to insert a new column [120, 140, 180]
before the column at index 1
along axis 1
(columns).[[ 1 120 2 3]
[ 4 140 5 6]
[ 7 180 8 9]]
new_arr
after the insertion operation. The new column [120, 140, 180]
has been inserted before the column [2, 5, 8]
at index 1
.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:
Hi, I am Vishal Jaiswal, I have about a decade of experience of working in MNCs like Genpact, Savista, Ingenious. Currently i am working in EXL as a senior quality analyst. Using my writing skills i want to share the experience i have gained and help as many as i can.
Python Practice Questions & Solutions Day 5 of Learning Python for Data Science Welcome back…
Day 5 of Learning Python for Data Science: Data Types, Typecasting, Indexing, and Slicing Understanding…
Python Practice Questions & Solutions Day 4 of Learning Python for Data Science Welcome back…
Day 4 of Learning Python for Data Science Day 4 of Learning Python for Data…
Test your Python skills with these 20 practice questions and solutions from Day 3 of…
Understanding Python’s conditional statements is essential for controlling the flow of a program. Today, we…