问题描述
例如,如果我具有如下2D数组.
For example, if I have the 2D array as follows.
[[1,2,3,NAN],
[4,5,NAN,NAN],
[6,NAN,NAN,NAN]
]
所需的结果是
[[1,2,3],
[4,5],
[6]
]
我应该如何转变?
我发现使用 x = x[~numpy.isnan(x)]
只能生成被压缩为一维数组的[1,2,3,4,5,6].
I find using x = x[~numpy.isnan(x)]
can only generate [1,2,3,4,5,6], which has been squeezed into one dimensional array.
谢谢!
推荐答案
只需逐行应用isnan
In [135]: [row[~np.isnan(row)] for row in arr]
Out[135]: [array([1., 2., 3.]), array([4., 5.]), array([6.])]
如x[~numpy.isnan(x)]
所述的布尔蒙版会产生平坦的结果,因为通常情况下,结果将像这样参差不齐,并且无法形成2d数组.
Boolean masking as in x[~numpy.isnan(x)]
produces a flattened result because, in general, the result will be ragged like this, and can't be formed into a 2d array.
源数组必须是float dtype-因为np.nan
是float:
The source array must be float dtype - because np.nan
is a float:
In [138]: arr = np.array([[1,2,3,np.nan],[4,5,np.nan,np.nan],[6,np.nan,np.nan,np.nan]])
In [139]: arr
Out[139]:
array([[ 1., 2., 3., nan],
[ 4., 5., nan, nan],
[ 6., nan, nan, nan]])
如果是object
dtype,则数字可以是整数,但np.isnan(arr)
无效.
If object
dtype, the numbers can be integer, but np.isnan(arr)
won't work.
如果原始文件是列表而不是数组:
If the original is a list, rather than an array:
In [146]: alist = [[1,2,3,np.nan],[4,5,np.nan,np.nan],[6,np.nan,np.nan,np.nan]]
In [147]: alist
Out[147]: [[1, 2, 3, nan], [4, 5, nan, nan], [6, nan, nan, nan]]
In [148]: [[i for i in row if ~np.isnan(i)] for row in alist]
Out[148]: [[1, 2, 3], [4, 5], [6]]
使用split
可以将平面数组转换为数组列表:
The flat array could be turned into a list of arrays with split
:
In [152]: np.split(arr[~np.isnan(arr)],(3,5))
Out[152]: [array([1., 2., 3.]), array([4., 5.]), array([6.])]
其中(3,5)
拆分参数可以通过对每行中的非nan进行计数来确定,但这需要更多工作,并且不能保证比行迭代要快.
where the (3,5)
split parameter could be determined by counting the non-nan in each row, but that's more work and doesn't promise to be faster than than the row iteration.
这篇关于从2D Numpy数组中删除NaN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!