# numpy.insert () in Python

| |

numpy.insert (array, object, values, axis = none): inserts values ​​along the specified axis before the specified indices.
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: removing from 1D array

 ` # Python program illustrating ` ` # numpy.insert () `   ` import ` ` numpy as geek `   ` # Working on 1D ` ` arr ` ` = ` ` geek.arange (` ` 5 ` `) ` ` print ` ` (` ` "1D arr:" ` `, arr ) ` ` print ` ` (` ` "Shape:" ` `, arr.shape) ` ` `  ` # value = 9 ` ` # index = 1 ` ` # Insert before first index ` ` a ` ` = ` ` geek.insert (arr, ` ` 1 ` `, ` ` 9 ` `) ` ` print ` ` ( ` ` "Array after insertion:" ` `, a) ` ` print ` ` (` `" Shape: "` `, a.shape) `     ` # Working with 2D array ` ` arr ` ` = ` ` geek.arange (` ` 12 ). reshape ( 3 , 4 ) `` print ( "2D arr:" , arr) print ( "Shape:" , arr.shape)   a = geek.insert (arr, 1 , 9 , axis = 1 ) print ( "Array after insertion:" , a) pri nt ( "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 program illustrating ` ` # numpy.insert () `   ` import ` ` numpy as geek ` ` `  ` # Working with 2D array ` ` arr ` ` = ` ` geek.arange (` ` 12 ` `). reshape (` ` 3 ` `, ` ` 4 ` `) ` ` print ` ` (` ` "2D arr:" ` `, arr) ` ` print ` ` (` ` "Shape:" ` `, arr.shape) `   ` # Working with scalars ` ` a ` ` = ` ` geek.insert (arr, [` ` 1 ` `], [[ ` ` 6 ` `], [` ` 9 ` `],], axis ` ` = ` ` 0 ` `) ` ` print ` ` (` ` "Array after insertion: "` `, a) ` ` print ` ` (` ` "Shape:" ` `, a.shape) `   ` # Working with scalars ` ` a ` ` = ` ` geek.insert (arr, [` ` 1 ` `] , [[` ` 8 ` `], [` ` 7 ` `], [` ` 9 ` `]], axis ` ` = ` ` 1 ` `) ` ` print ` ` (` ` "Array after insertion:" ` `, 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: insert at different points

` `

` # Python program illustrating # numpy.insert ()   import numpy as geek   # Working on 1D arr = geek.arange ( 6 ). Reshape ( 2 , 3 ) print ( " 1D arr: " , arr) print ( "Shape:" , arr.shape )   # value = 9 # index = 1 # Insert before the first index a = geek.insert (arr, ( 2 , 4 ), 9 ) print ( "Insertion at two points:" , a) print ( "Shape:" , a.shape)      # Working with 2D array arr = geek.arange ( 12 ) .reshape ( 3 , 4 ) print ( "2D arr:" , arr) print ( "Shape:" , arr.shape) a = geek.insert (arr, ( 0 , 3 ), 66 , axis = 1 ) print ( "Insertion at two points:" , 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) `

Notes :
These codes will not work for online IDs. Please run them on your systems to see how they work.

## Shop Learn programming in R: courses

\$ Best Python online courses for 2022

\$ Best laptop for Fortnite

\$ Best laptop for Excel

\$ Best laptop for Solidworks

\$ Best laptop for Roblox

\$ Best computer for crypto mining

\$ Best laptop for Sims 4

\$

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

NUMPYNUMPY

How to convert Nonetype to int or string?

NUMPYNUMPY

How to specify multiple return types using type-hints

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

## Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method