在Python中将“nan”值移动到数组的开头

6
如果我有一个包含NaN的数组,它看起来像这样:
array([[ 0.,  0.,  0.,  0.],
       [ 0.,  0., nan, nan],
       [ 0.,  1.,  3., nan],
       [ 0.,  2.,  4.,  7.],
       [ 0., nan,  2., nan],
       [ 0.,  4., nan, nan]])

如何将数组中所有的NaN值都移到数组的开头,同时不改变数组的形状?类似于以下操作:

array([[ 0.,  0.,  0.,  0.],
       [ nan, nan, 0.,  0.],
       [ nan, 0.,  1.,  3.],
       [ 0.,  2.,  4.,  7.],
       [ nan, nan, 0.,  2.],
       [ nan, nan, 0.,  4.]])

很酷的问题。编写一个解决方案并不难,但我期待着利用numpy实现高性能的答案。 - timgeb
1个回答

4

以下是一种方式:

# find the position of nan itms in "a"
In [19]: mask = np.isnan(a)                                                                                                                                                                                 
# put them at the beginning by sorting the mask in a descending order
In [20]: nan_pos = np.sort(mask)[:,::-1]                                                                                                                                                                    
# the new position of non_non items is the inverse of non-mask sorted ascending 
In [21]: not_nan_pos = np.sort(~mask)                                                                                                                                                                       

In [22]: emp = np.empty(a.shape)                                                                                                                                                                            

In [23]: emp[nan_pos] = np.nan                                                                                                                                                                              

In [24]: emp[not_nan_pos] = a[~mask]                                                                                                                                                                        

In [25]: emp                                                                                                                                                                                                
Out[25]: 
array([[ 0.,  0.,  0.,  0.],
       [nan, nan,  0.,  0.],
       [nan,  0.,  1.,  3.],
       [ 0.,  2.,  4.,  7.],
       [nan, nan,  0.,  2.],
       [nan, nan,  0.,  4.]])

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接