Kurt Peek - 4 months ago 11

Python Question

Starting with the following array

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

which is generated like so:

`import numpy as np`

row = np.array([ np.nan, np.nan, np.nan, 1., np.nan, np.nan, 0., np.nan, np.nan])

I'd like to get the indices of the sorted array and then exclude the

`nans`

`[6,3]`

I've come up with the following way to do this:

`vals = np.sort(row)`

inds = np.argsort(row)

def select_index_by_value(indices, values):

selected_indices = []

for i in range(len(indices)):

if not np.isnan(values[i]):

selected_indices.append(indices[i])

return selected_indices

selected_inds = select_index_by_value(inds, vals)

Now

`selected_inds`

`[6,3]`

Source (Stackoverflow)

Comments