Use np.insert
import numpy as np
np.insert(array, index, number)
Answer from sushanth on Stack Overflowpython - Adding index to numpy arrays - Stack Overflow
arrays - How to add an element at the index using numpy in python - Stack Overflow
Python Numpy add an array at array index - Stack Overflow
Trying to add 1 to the element at a certain index of a numpy array
Use np.insert
import numpy as np
np.insert(array, index, number)
You can use the numpy.insert function to insert a value at a specified point in a numpy.array. Here is how you would use it in your case:
array = np.array([ 31, 28, 31, 30, 31, 30, 31, 31])
np.insert(array, 5, 3)
The second argument is the index before which you wish to insert the value, which is the third argument. See the documentation here for more information, especially for higher-dimensional arrays, which can get a bit more complicated.
I’ve created some np.zeros arrays but I want to add 1 to some of the zeros at certain index’s. Does anyone know a way to do this as I currently can’t find anything anywhere. Or will I have to create a list full of 0s and then convert it to a np array once I have manipulated it the way I want? Would go from 0,0,0,0,0,0,0 To looking something like: 0,2,0,0,1,1,3
Any help appreciated.