逐行移动numpy数组 [英] shift numpy array by row

查看:382
本文介绍了逐行移动numpy数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

数组:

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

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

我按如下方式使用scipy.ndimage.interpolation中的shift:

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.]])

但是,我想要:

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

基本上,我想将所有列数据向下移动,并从数据集中引导最后一行. Pandas具有可以执行此操作的移位功能,但我不确定如何在Numpy中完成它.

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.

推荐答案

您可以从scipy.ndimage.interpolation更改shift函数的shift参数(第二个参数),如下所示:

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)

在这里,(1, 0)表示在第一个维度上移动1,在第二个维度上移动0.

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

这篇关于逐行移动numpy数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆