问题
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
.
回答1:
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.
来源:https://stackoverflow.com/questions/58616688/shift-numpy-array-by-row