Shift all indices in NumPy array

0 votes
I have the following numpy array:

x=np.array([0,1,2,3,4])
 

and want to create an array where the value in index 0 is in index 1, index 1 is in index 2, etc.

The output I want is:

y=np.array([0,0,1,2,3]).
 

How can I do this in a numPythonic way? Can anyone help?
Feb 26 in Python by ana1504.k
• 7,890 points
523 views

1 answer to this question.

0 votes
You can use the following :

y = numpy.roll(x, 1)
y[0] = 0
 

or

y = numpy.r_[0, x[:-1]]
answered Feb 26 by SDeb
• 13,190 points

Related Questions In Python

0 votes
1 answer

How to save Numpy array as image in python?

If you have matplotlib, you can do: import ...READ MORE

answered Nov 16, 2018 in Python by Nymeria
• 3,520 points
2,456 views
0 votes
1 answer

Is it possible to create an array with all values as zero in python?

You can use  np.zeros(4,3) This will create a 4 ...READ MORE

answered May 24 in Python by Anjali
57 views
+1 vote
2 answers

View onto a numpy array?

 just index it as you normally would. ...READ MORE

answered Oct 18, 2018 in Python by roberto
57 views
0 votes
1 answer
0 votes
1 answer

Printing a large numpy array

numpy.set_printoptions(threshold='nan') READ MORE

answered Jul 20, 2018 in Python by Nietzsche's daemon
• 4,260 points
128 views
0 votes
1 answer

Dictionary in NumPy array

You have a 0-dimensional array of object ...READ MORE

answered Jan 18 in Python by SDeb
• 13,190 points
1,411 views
0 votes
1 answer

Is arr.__len__() the preferred way to get the length of an array in Python?

my_list = [1,2,3,4,5,6,7] len(my_list) # 7 The same works for ...READ MORE

answered Oct 8, 2018 in Python by SDeb
• 13,190 points
77 views