codingknob
codingknob

Reputation: 11660

shift numpy array by row

Array:

arr = np.ones([4,4])

array([[ 1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.]])

I use shift from scipy.ndimage.interpolation as follows:

shift(arr,1, cval=np.nan)

array([[ nan,  nan,  nan,  nan],
       [ nan,   1.,   1.,   1.],
       [ nan,   1.,   1.,   1.],
       [ nan,   1.,   1.,   1.]])

HOWEVER, I want:

array([[ nan,  nan,  nan,  nan],
       [ 1.,   1.,   1.,   1.],
       [ 1.,   1.,   1.,   1.],
       [ 1.,   1.,   1.,   1.]])

Basically, I want to SHIFT all columns data down the rows and boot the last row out of my data set. Pandas has the shift function that can do this but I'm not certain how it can be done in Numpy.

Upvotes: 0

Views: 404

Answers (1)

Stanislas Morbieu
Stanislas Morbieu

Reputation: 1827

You can change the shift parameter (second parameter) of the shift function from scipy.ndimage.interpolation as follows:

shift(arr, (1, 0), cval=np.nan)

Here, (1, 0) means a shift of 1 in the first dimension, and 0 in the second dimension.

Upvotes: 2

Related Questions