numpy has an insert function that's accesible via np.insert with documentation.

You'll want to use it in this case like so:

X = np.insert(X, 0, 6., axis=0)

the first argument X specifies the object to be inserted into.

The second argument 0 specifies where.

The third argument 6. specifies what is to be inserted.

The fourth argument axis=0 specifies that the insertion should happen at position 0 for every column. We could've chosen rows but your X is a columns vector, so I figured we'd stay consistent.

Answer from Rosey on Stack Overflow
🌐
Medium
medium.com › @whyamit404 › understanding-numpy-prepend-with-examples-72fdb55900a6
Understanding numpy.prepend with Examples | by whyamit404 | Medium
February 26, 2025 - Without the brackets, it’ll throw an error because 1 by itself isn’t an array. ... If concatenate feels like stitching fabric, think of insert as sliding a bookmark into your favorite novel. It lets you insert elements exactly where you want—whether that’s at the beginning, middle, or end. ... import numpy as np # Original array arr = np.array([5, 6, 7]) # Prepending elements 3 and 4 at index 0 new_arr = np.insert(arr, 0, [3, 4]) print(new_arr) # Output: [3 4 5 6 7]
🌐
Note.nkmk.me
note.nkmk.me › home › python › numpy
NumPy: append() to add values to an array | note.nkmk.me
February 4, 2024 - In NumPy, the np.append() function allows you to add values (elements, rows, or columns) to either the end or the beginning of an array (ndarray). numpy.append — NumPy v1.26 Manual Note that append() ...
🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.insert.html
numpy.insert — NumPy v2.1 Manual
Input array. ... Object that defines the index or indices before which values is inserted. New in version 1.8.0. Support for multiple insertions when obj is a single scalar or a sequence with one element (similar to calling insert multiple times).
🌐
Codegive
codegive.com › blog › numpy_add_element_to_beginning_of_array.php
Numpy add element to beginning of array
If you wanted to "add" 0 to the beginning, NumPy can't just slide 1, 2, and 3 over one slot in the existing memory block. It needs to: Allocate a new block of memory large enough for the new array (e.g., 4 elements). Place the new element (0) at the beginning of this new block.
🌐
Machinelearninghelp
machinelearninghelp.org › programming-for-machine-learning › how-to-add-element-to-beginning-of-array-python
Adding Elements to the Beginning of an Array in Python
Here, we use np.insert() to add a new element at the beginning of our NumPy array. This approach is generally more memory-efficient for large datasets. While not directly applicable in this context, understanding how arrays work under the hood can enhance your programming skills. In Python, lists ...
🌐
Statology
statology.org › home › how to add elements to numpy array (3 examples)
How to Add Elements to NumPy Array (3 Examples)
June 15, 2022 - You can use the following methods to add one or more elements to a NumPy array: ... #insert 95 and 99 starting at index position 2 of the NumPy array new_array = np.insert(my_array, 2, [95, 99])
Find elsewhere
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.insert.html
numpy.insert — NumPy v2.5 Manual
Input array. ... 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.
🌐
TutorialsPoint
tutorialspoint.com › numpy › numpy_insert.htm
Numpy insert() Function
Here in this example we are passing the axis parameter to the insert() function and broadcast to match the input array − · import numpy as np # Define the initial array b = np.array([[1, 2], [3, 4], [5, 6], [7, 8]]) print('First array:') ...
🌐
Note.nkmk.me
note.nkmk.me › home › python › numpy
NumPy: Insert elements, rows, and columns into an array with np.insert() | note.nkmk.me
March 22, 2023 - To add rows to the beginning or end of an array rather than in the middle, use np.vstack() to vertically concatenate arrays. You can use either a 1D array with the same number of elements as the original array's number of columns or a 2D array ...
🌐
Medium
panjeh.medium.com › numpy-insert-prepend-element-to-numpy-array-with-axis-parameter-875296ed87af
numpy.insert python prepend element to numpy array with axis parameter | by Panjeh | Medium
June 14, 2020 - import numpy as np a = np.array([[1, 11], [2, 22], [3, 33]]) print('Original array:') print(a) print()print("axis=1") x= np.insert(a, 0, 6, axis=1) print(x) print()print("axis=0") x= np.insert(a, 2, 5, axis=0) print(x)
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.append.html
numpy.append — NumPy v2.5 Manual
A copy of arr with values appended to axis. Note that append does not occur in-place: a new array is allocated and filled. If axis is None, out is a flattened array. ... Insert elements into an array.
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-add-to-array
Python Array Add: How to Append, Extend & Insert Elements | DigitalOcean
Learn how to add to an array in Python with append(), extend(), insert(), and NumPy. Compare lists vs arrays, avoid common errors, and see tested examples.
🌐
Vultr Docs
docs.vultr.com › python › third party › numpy › insert()
Python Numpy insert() - Insert Elements
November 15, 2024 - Define an initial array. Use numpy.insert() to add an element at a desired index.
🌐
Nanyang Technological University
libguides.ntu.edu.sg › python › insertelementsintoarrays
NP.9 Inserting elements into arrays - Python for Basic Data Analysis - LibGuides at Nanyang Technological University
August 26, 2026 - Let's add 11,12 and 13 between the 2nd and 3rd elements. First, we first add square brackets around the numbers to be added. Next, we specify the index 2, to insert these numbers before the third element. To work with 2-D arrays, we cannot simply add a random number of elements.
🌐
NumPy
numpy.org › devdocs › reference › generated › numpy.append.html
numpy.append — NumPy v2.6.dev0 Manual
A copy of arr with values appended to axis. Note that append does not occur in-place: a new array is allocated and filled. If axis is None, out is a flattened array. ... Insert elements into an array.
🌐
Centron
centron.de › home › tutorials › how to add elements to an array in python
How To Add Elements to an Array in Python – centron
December 6, 2024 - When the axis is 1, the array is appended by column. The numpy.insert() function inserts an array or values into another array before the given index, along an axis, and returns a new array.