Shift elements in a Numpy Array

Last Updated On Thursday 11th Nov 2021

numpy roll

np roll

The numpy.roll() function rolls array elements along the specified axis.If an element is being rolled first to the last position, it is rolled back to the first position.

	numpy.roll(a, shift, axis=None)
	

python np roll

Parameters

  • a : array_like – Input array.
  • shift : int or tuple of ints – The number of places by which elements are shifted. If a tuple, then axis must be a tuple of the same size, and each of the given axes is shifted by the corresponding number. If an int while axis is a tuple of ints, then the same value is used for all given axes.
  • axis : int or tuple of ints, optional
    Axis or axes along which elements are shifted. By default, the array is flattened before shifting, after which the original shape is restored.

python shift array

numpy shift

NumPy roll() function is defined under NumPy, which can be imported as import NumPy as np, and we can create multidimensional arrays and derive other mathematical statistics with the help of NumPy, which is a library in Python

	import numpy as np

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

op_arr = np.roll(array, 2)
print(op_arr)
	
  • We first created the array with the np.array() function.
  • We then shifted the elements towards the right with the np.roll() function and stored the resultant array inside op_arr.
	[4 5 1 2 3]
	

array shift python

NumPy Shift Array With the Slicing Method in Python

	import numpy as np
array = np.array([1,2,3,4,5])
num = 3
fill_value = 0

def shift(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(array, num, fill_value)
	

python array shift right

  • We have to use the array slicing method in Python
  • If the shift value is positive, we fill the left side of the array with the constant value.
  • If the shift value is negative, we fill the right side of the array with the constant value.
  • num – Number of Indices We want to shift our array elements
  • fill_value – Replace all the shifted indices with.
	[0, 0, 0, 1, 2]
	

python shift elements in list

	import numpy as np
array = np.array([1,2,3,4,5])
num = 3
fill_value = 0

def shift(arr, num, fill_value=np.nan):
    if num >= 0:
        return np.concatenate((np.full(num, fill_value), arr[:-num]))
    else:
        return np.concatenate((arr[-num:], np.full(-num, fill_value)))

print(shift(array, num, fill_value))
	

np.concatenate – Concatenation refers to joining. This function is used to join two or more arrays of the same shape along a specified axis. The function takes the following parameters.

  • num – Number of Indices We want to shift our array elements
  • fill_value – Replace all the shifted indices with.
	numpy.concatenate((a1, a2,...), axis)
	
	[0, 0, 0, 1, 2]