I have a numpy array like
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)
Now I want to sort the values with key as isnan? How can I do that? So that I would end up in the array
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()
didn't work. The same can be achieved in pandas by applying sorted over columns with sorted
function with key as pd.isnull(), but looking for a numpy answer for speed.
In pandas
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
Output :
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)
See Question&Answers more detail:
os