The numpy.insert() function inserts values along the mentioned axis before the given indices. Syntax :
numpy.insert(array, object, values, axis = None)
Parameters :
array : [array_like]Input array.
object : [int, array of ints]Sub-array with the index or indices before
which values is inserted
values : [array_like]values to be added in the arr. Values should be
shaped so that arr[...,obj,...] = values. If the type of values is different from
that of arr, values is converted to the type of arr
axis : Axis along which we want to insert the values. By default, it
object is applied to flattened array
Return :
An copy of array with values being inserted as per the mentioned object along a given axis.
Code 1 : Deletion from 1D array
Python
# Python Program illustrating
# numpy.insert()
import numpy as geek
#Working on 1D
arr = geek.arange(5)
print("1D arr : \n", arr)
print("Shape : ", arr.shape)
# value = 9
# index = 1
# Insertion before first index
a = geek.insert(arr, 1, 9)
print("\nArray after insertion : ", a)
print("Shape : ", a.shape)
# Working on 2D array
arr = geek.arange(12).reshape(3, 4)
print("\n\n2D arr : \n", arr)
print("Shape : ", arr.shape)
a = geek.insert(arr, 1, 9, axis = 1)
print("\nArray after insertion : \n", a)
print("Shape : ", a.shape)
Output :
1D arr :
[0 1 2 3 4]
Shape : (5,)
Array after insertion : [0 9 1 2 3 4]
Shape : (6,)
2D arr :
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
Shape : (3, 4)
Array after insertion :
[[ 0 9 1 2 3]
[ 4 9 5 6 7]
[ 8 9 9 10 11]]
Shape : (3, 5)
Code 2 : Working with Scalars
Python
# Python Program illustrating
# numpy.insert()
import numpy as geek
# Working on 2D array
arr = geek.arange(12).reshape(3, 4)
print("2D arr : \n", arr)
print("Shape : ", arr.shape)
# Working with Scalars
a = geek.insert(arr, [1], [[6],[9],], axis = 0)
print("\nArray after insertion : \n", a)
print("Shape : ", a.shape)
# Working with Scalars
a = geek.insert(arr, [1], [[8],[7],[9]], axis = 1)
print("\nArray after insertion : \n", a)
print("Shape : ", a.shape)
Output :
2D arr :
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
Shape : (3, 4)
Array after insertion :
[[ 0 1 2 3]
[ 6 6 6 6]
[ 9 9 9 9]
[ 4 5 6 7]
[ 8 9 10 11]]
Shape : (5, 4)
Array after insertion :
[[ 0 8 1 2 3]
[ 4 7 5 6 7]
[ 8 9 9 10 11]]
Shape : (3, 5)
Code 3 : Insertion at different points
Python
# Python Program illustrating
# numpy.insert()
import numpy as geek
#Working on 1D
arr = geek.arange(6).reshape(2, 3)
print("1D arr : \n", arr)
print("Shape : ", arr.shape)
# value = 9
# index = 1
# Insertion before first index
a = geek.insert(arr, (2, 4), 9)
print("\nInsertion at two points : ", a)
print("Shape : ", a.shape)
# Working on 2D array
arr = geek.arange(12).reshape(3, 4)
print("\n\n2D arr : \n", arr)
print("Shape : ", arr.shape)
a = geek.insert(arr, (0, 3), 66, axis = 1)
print("\nInsertion at two points : \n", a)
print("Shape : ", a.shape)
Output :
1D arr :
[[0 1 2]
[3 4 5]]
Shape : (2, 3)
Insertion at two points : [0 1 9 2 3 9 4 5]
Shape : (8,)
2D arr :
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
Shape : (3, 4)
Insertion at two points :
[[66 0 1 2 66 3]
[66 4 5 6 66 7]
[66 8 9 10 66 11]]
Shape : (3, 6)
References : https://docs.scipy.org/doc/numpy/reference/generated/numpy.insert.html#numpy.insert Note : These codes won’t run on online IDE's. Please run them on your systems to explore the working.
Similar Reads
numpy.add() in Python NumPy, the Python powerhouse for scientific computing, provides an array of tools to efficiently manipulate and analyze data. Among its key functionalities lies numpy.add() a potent function that performs element-wise addition on NumPy arrays. numpy.add() SyntaxSyntax :Â numpy.add(arr1, arr2, /, out=
4 min read
numpy.append() in Python numpy.append() function is used to add new values at end of existing NumPy array. This is useful when we have to add more elements or rows in existing numpy array. It can also combine two arrays into a bigger one. Syntax: numpy.append(array, values, axis = None)array: Input array. values: The values
2 min read
Python | Pandas Index.insert() Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas Index.insert() function make new Index inserting new item at location. This fun
2 min read
numpy.ndarray.fill() in Python numpy.ndarray.fill() method is used to fill the numpy array with a scalar value. If we have to initialize a numpy array with an identical value then we use numpy.ndarray.fill(). Suppose we have to create a NumPy array a of length n, each element of which is v. Then we use this function as a.fill(v).
2 min read
Input and Output in Python Understanding input and output operations is fundamental to Python programming. With the print() function, we can display output in various formats, while the input() function enables interaction with users by gathering input during program execution. Taking input in PythonPython input() function is
8 min read
Python MySQL - Insert into Table MySQL is a Relational Database Management System (RDBMS) whereas the structured Query Language (SQL) is the language used for handling the RDBMS using commands i.e Creating, Inserting, Updating and Deleting the data from the databases. SQL commands are case insensitive i.e CREATE and create signify
3 min read
Insertion Sort - Python Insertion sort is a simple sorting algorithm that works by iteratively inserting each element of an unsorted list into its correct position in a sorted portion of the list.Insertion SortThe insertionSort function takes an array arr as input. It first calculates the length of the array (n). If the le
3 min read
numpy.char.add() function in Python The add() method of the char class in the NumPy module is used for element-wise string concatenation for two arrays of str or unicode. numpy.char.add()Syntax : numpy.char.add(x1, x2)Parameters : x1 : first array to be concatenated (concatenated at the beginning)x2 : second array to be concatenated (
1 min read
Python | Pandas dataframe.insert() Pandas insert method allows the user to insert a column in a data frame or series(1-D Data frame). A column can also be inserted manually in a data frame by the following method, but there isn't much freedom here. For example, even column location can't be decided and hence the inserted column is al
8 min read
Python | sympy.Matrix().col_insert() With the help of Matrix().col_insert() method, we can insert a column in a matrix having dimension nxm, where dimension of inserted column is nx1. Syntax : Matrix().col_insert() Return : Return a new matrix. Example #1 : In this example, we are able to insert a column in a matrix by using Matrix().c
1 min read