user308827
user308827

Reputation: 21961

Reordering numpy array

I have the following Numpy array:

arr = np.array([0.3, 3.5, 12.0, 2.9, 11.0, 23.0])

I want to reorder the array so it starts at the 4th position, followed by the items after the start position in order, followed by the items before the start position. I.e.

[2.9, 11.0, 23.0, 0.3, 3.5, 12.0]

How can I do this without a for loop?

Upvotes: 1

Views: 289

Answers (2)

saladi
saladi

Reputation: 3253

Try

np.roll(arr, -3)

Negative since you want to "move" elements to the left

Upvotes: 4

roadrunner66
roadrunner66

Reputation: 7941

The command you are looking for is numpy.roll. It's the equivalent of Mathematica's Rotate command.

Upvotes: 3

Related Questions