Idr Idr - 2 years ago 163
Python Question

Shift all indices in NumPy array

I have a numpy array like this:


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:


I'm guessing there's an easy way to do this without iterating through the full array. How can I do this in a numPythonic way?

Answer Source

You can use

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


y = numpy.r_[0, x[:-1]]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download