我有个核阵列
np.array([[1.0, np.nan, 5.0, 1, True, True, np.nan, True],
[np.nan, 4.0, 7.0, 2, True, np.nan, False, True],
[2.0, 5.0, np.nan, 3, False, False, True, np.nan]], dtype=object)
现在我想用键isnan对值进行排序我该怎么做?这样我就可以排成一排了
np.array([[1.0, 5.0, 1, True, True, True, np.nan, np.nan],
[4.0, 7.0, 2, True, False, True, np.nan, np.nan],
[2.0, 5.0, 3, False, False, True, np.nan, np.nan]], dtype=object)
np.sort()
不起作用在pandas中也可以通过使用sorted
函数对列应用sorted over columns,键为pd.isnull(),但要寻找速度的numpy答案。在熊猫中
data = pd.DataFrame({'Key': [1, 2, 3], 'Var': [True, True, False], 'ID_1':[1, np.NaN, 2],
'Var_1': [True, np.NaN, False], 'ID_2': [np.NaN, 4, 5], 'Var_2': [np.NaN, False, True],
'ID_3': [5, 7, np.NaN], 'Var_3': [True, True, np.NaN]})
data.apply(lambda x : sorted(x,key=pd.isnull),1).values
输出:
array([[1.0, 5.0, 1, True, True, True, nan, nan],
[4.0, 7.0, 2, True, False, True, nan, nan],
[2.0, 5.0, 3, False, False, True, nan, nan]], dtype=object)
最佳答案
方法1
这里有一个向量化的方法从masking
中借用this post
的概念-
def mask_app(a):
out = np.empty_like(a)
mask = np.isnan(a.astype(float))
mask_sorted = np.sort(mask,1)
out[mask_sorted] = a[mask]
out[~mask_sorted] = a[~mask]
return out
样本运行-
# Input dataframe
In [114]: data
Out[114]:
ID_1 ID_2 ID_3 Key Var Var_1 Var_2 Var_3
0 1.0 NaN 5.0 1 True True NaN True
1 NaN 4.0 7.0 2 True NaN False True
2 2.0 5.0 NaN 3 False False True NaN
# Use pandas approach for verification
In [115]: data.apply(lambda x : sorted(x,key=pd.isnull),1).values
Out[115]:
array([[1.0, 5.0, 1, True, True, True, nan, nan],
[4.0, 7.0, 2, True, False, True, nan, nan],
[2.0, 5.0, 3, False, False, True, nan, nan]], dtype=object)
# Use proposed approach and verify
In [116]: mask_app(data.values)
Out[116]:
array([[1.0, 5.0, 1, True, True, True, nan, nan],
[4.0, 7.0, 2, True, False, True, nan, nan],
[2.0, 5.0, 3, False, False, True, nan, nan]], dtype=object)
方法2
经过很少的修改,一个简化版本的想法从
this post
-def mask_app2(a):
out = np.full(a.shape,np.nan,dtype=a.dtype)
mask = ~np.isnan(a.astype(float))
out[np.sort(mask,1)[:,::-1]] = a[mask]
return out