How to Shift Elements in NumPy Array (With Examples)


You can use one of the following methods to shift the elements in a NumPy array:

Method 1: Shift Elements (Keep All Original Elements)

#shift each element two positions to the right
data_new = np.roll(data, 2)

Method 2: Shift Elements (Allow Elements to Be Replaced)

#define shifting function
def shift_elements(arr, num, fill_value):
    result = np.empty_like(arr)
    if num > 0:
        result[:num] = fill_value
        result[num:] = arr[:-num]
    elif num < 0:
        result[num:] = fill_value
        result[:num] = arr[-num:]
    else:
        result[:] = arr
    return result

#shift each element two positions to the right (replace shifted elements with zero)
data_new = shift_elements(data, 2, 0)

The following examples show how to use each method in practice.

Method 1: Shift Elements (Keep All Original Elements)

The following code shows how to use the np.roll() function to shift each element in a NumPy array two positions to the right:

import numpy as np

#create NumPy array
data = np.array([1, 2, 3, 4, 5, 6])

#shift each element two positions to the right
data_new = np.roll(data, 2)

#view new NumPy array
data_new

array([5, 6, 1, 2, 3, 4])

Notice that each element was shifted two positions to the right and elements at the end of the array simply got moved to the front.

We could also use a negative number in the np.roll() function to shift elements to the left:

import numpy as np

#create NumPy array
data = np.array([1, 2, 3, 4, 5, 6])

#shift each element three positions to the left
data_new = np.roll(data, -3)

#view new NumPy array
data_new

array([4, 5, 6, 1, 2, 3])

Method 2: Shift Elements (Allow Elements to Be Replaced)

We can also define a custom function to shift the elements in a NumPy array and allow elements that are shifted to be replaced by a certain value.

For example, we can define the following function to shift elements and replace any shifted elements with the value 0:

import numpy as np

#create NumPy array
data = np.array([1, 2, 3, 4, 5, 6])

#define custom function to shift elements
def shift_elements(arr, num, fill_value):
    result = np.empty_like(arr)
    if num > 0:
        result[:num] = fill_value
        result[num:] = arr[:-num]
    elif num < 0:
        result[num:] = fill_value
        result[:num] = arr[-num:]
    else:
        result[:] = arr
    return result

#shift each element two positions to the right and replace shifted values with zero
data_new = shift_elements(data, 2, 0)

#view new NumPy array
data_new

array([0, 0, 1, 2, 3, 4])

We can also use a negative number in the function to shift the elements to the left:

import numpy as np

#create NumPy array
data = np.array([1, 2, 3, 4, 5, 6])

#define custom function to shift elements
def shift_elements(arr, num, fill_value):
    result = np.empty_like(arr)
    if num > 0:
        result[:num] = fill_value
        result[num:] = arr[:-num]
    elif num < 0:
        result[num:] = fill_value
        result[:num] = arr[-num:]
    else:
        result[:] = arr
    return result

#shift each element three positions to the left and replace shifted values with 50
data_new = shift_elements(data, -3, 50)

#view new NumPy array
data_new

array([ 4,  5,  6, 50, 50, 50])

Additional Resources

The following tutorials explain how to perform other common operations in NumPy:

How to Count Occurrences of Elements in NumPy
How to Sort a NumPy Array by Column
How to Calculate the Mode of NumPy Array

Leave a Reply

Your email address will not be published.