How is it possible to shift the elements of a 2D numpy array in Python?
For example convert this array :
[[ 1, 2, 3],
[ 4, 5, 6],
[ 7, 8, 9],
[10, 11, 12]]
to this array:
[[0, 0, 0,],
[ 1, 2, 3],
[ 4, 5, 6],
[ 7, 8, 9]]
import numpy as np
a = np.array([[ 1, 2, 3],
[ 4, 5, 6],
[ 7, 8, 9],
[10, 11, 12]])
def shift_array(array, place):
new_arr = np.roll(array, place, axis=0)
new_arr[:place] = np.zeros((new_arr[:place].shape))
return new_arr
shift_array(a,2)
# array([[ 0, 0, 0],
# [ 0, 0, 0],
# [ 1, 2, 3],
# [ 4, 5, 6]])