Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Numpy exercise
  • pandas
  • Matplotlib
  • Data visulisation
  • EDA
  • Machin Learning
  • Deep Learning
  • NLP
  • Data science
  • ML Tutorial
  • Computer Vision
  • ML project
Open In App
Next Article:
numpy.insert() in Python
Next article icon

numpy.insert() in Python

Last Updated : 28 Mar, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

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.


Next Article
numpy.insert() in Python

M

Mohit Gupta
Improve
Article Tags :
  • Python
  • Python-numpy
  • Python numpy-arrayManipulation
Practice Tags :
  • python

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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences