sert — NumPy V1.23 Manual

Skip to main content Back to top Ctrl+K
  • User Guide
  • API reference
  • Building from source
  • Development
  • Release notes
  • Learn
  • NEPs
Choose version
  • GitHub

Section Navigation

  • NumPy’s module structure
  • Array objects
  • Universal functions (ufunc)
  • Routines and objects by topic
    • Constants
    • Array creation routines
    • Array manipulation routines
      • numpy.copyto
      • numpy.ndim
      • numpy.shape
      • numpy.size
      • numpy.reshape
      • numpy.ravel
      • numpy.ndarray.flat
      • numpy.ndarray.flatten
      • numpy.moveaxis
      • numpy.rollaxis
      • numpy.swapaxes
      • numpy.ndarray.T
      • numpy.transpose
      • numpy.permute_dims
      • numpy.matrix_transpose
      • numpy.atleast_1d
      • numpy.atleast_2d
      • numpy.atleast_3d
      • numpy.broadcast
      • numpy.broadcast_to
      • numpy.broadcast_arrays
      • numpy.expand_dims
      • numpy.squeeze
      • numpy.asarray
      • numpy.asanyarray
      • numpy.asmatrix
      • numpy.asfortranarray
      • numpy.ascontiguousarray
      • numpy.asarray_chkfinite
      • numpy.require
      • numpy.concatenate
      • numpy.concat
      • numpy.stack
      • numpy.block
      • numpy.vstack
      • numpy.hstack
      • numpy.dstack
      • numpy.column_stack
      • numpy.split
      • numpy.array_split
      • numpy.dsplit
      • numpy.hsplit
      • numpy.vsplit
      • numpy.unstack
      • numpy.tile
      • numpy.repeat
      • numpy.delete
      • numpy.insert
      • numpy.append
      • numpy.resize
      • numpy.trim_zeros
      • numpy.unique
      • numpy.pad
      • numpy.flip
      • numpy.fliplr
      • numpy.flipud
      • numpy.roll
      • numpy.rot90
    • Bit-wise operations
    • String functionality
    • Datetime support functions
    • Data type routines
    • Mathematical functions with automatic domain
    • Floating point error handling
    • Exceptions and Warnings
    • Discrete Fourier Transform
    • Functional programming
    • Input and output
    • Indexing routines
    • Linear algebra
    • Logic functions
    • Masked array operations
    • Mathematical functions
    • Miscellaneous routines
    • Polynomials
    • Random sampling
    • Set routines
    • Sorting, searching, and counting
    • Statistics
    • Test support
    • Window functions
  • Typing (numpy.typing)
  • Packaging
  • NumPy C-API
  • Array API standard compatibility
  • CPU/SIMD optimizations
  • Thread Safety
  • Global Configuration Options
  • NumPy security
  • Testing guidelines
  • Status of numpy.distutils and migration advice
  • numpy.distutils user guide
  • NumPy and SWIG
  • NumPy reference
  • Routines and objects by topic
  • Array manipulation routines
  • numpy.insert
numpy.insert# numpy.insert(arr, obj, values, axis=None)[source]#

Insert values along the given axis before the given indices.

Parameters: arrarray_like

Input array.

objslice, int, array-like of ints or bools

Object that defines the index or indices before which values is inserted.

Changed in version 2.1.2: Boolean indices are now treated as a mask of elements to insert, rather than being cast to the integers 0 and 1.

Support for multiple insertions when obj is a single scalar or a sequence with one element (similar to calling insert multiple times).

valuesarray_like

Values to insert into arr. If the type of values is different from that of arr, values is converted to the type of arr. values should be shaped so that arr[...,obj,...] = values is legal.

axisint, optional

Axis along which to insert values. If axis is None then arr is flattened first.

Returns: outndarray

A copy of arr with values inserted. Note that insert does not occur in-place: a new array is returned. If axis is None, out is a flattened array.

See also

append

Append elements at the end of an array.

concatenate

Join a sequence of arrays along an existing axis.

delete

Delete elements from an array.

Notes

Note that for higher dimensional inserts obj=0 behaves very different from obj=[0] just like arr[:,0,:] = values is different from arr[:,[0],:] = values. This is because of the difference between basic and advanced indexing.

Examples

Try it in your browser! >>> importnumpyasnp >>> a = np.arange(6).reshape(3, 2) >>> a array([[0, 1], [2, 3], [4, 5]]) >>> np.insert(a, 1, 6) array([0, 6, 1, 2, 3, 4, 5]) >>> np.insert(a, 1, 6, axis=1) array([[0, 6, 1], [2, 6, 3], [4, 6, 5]])

Difference between sequence and scalars, showing how obj=[1] behaves different from obj=1:

>>> np.insert(a, [1], [[7],[8],[9]], axis=1) array([[0, 7, 1], [2, 8, 3], [4, 9, 5]]) >>> np.insert(a, 1, [[7],[8],[9]], axis=1) array([[0, 7, 8, 9, 1], [2, 7, 8, 9, 3], [4, 7, 8, 9, 5]]) >>> np.array_equal(np.insert(a, 1, [7, 8, 9], axis=1), ... np.insert(a, [1], [[7],[8],[9]], axis=1)) True >>> b = a.flatten() >>> b array([0, 1, 2, 3, 4, 5]) >>> np.insert(b, [2, 2], [6, 7]) array([0, 1, 6, 7, 2, 3, 4, 5]) >>> np.insert(b, slice(2, 4), [7, 8]) array([0, 1, 7, 2, 8, 3, 4, 5]) >>> np.insert(b, [2, 2], [7.13, False]) # type casting array([0, 1, 7, 0, 2, 3, 4, 5]) >>> x = np.arange(8).reshape(2, 4) >>> idx = (1, 3) >>> np.insert(x, idx, 999, axis=1) array([[ 0, 999, 1, 2, 999, 3], [ 4, 999, 5, 6, 999, 7]]) Go BackOpen In Tab On this page
  • insert

Tag » Add Element In Matrix Python