1

I have a numpy array that contains strings.

index1 = ['level4','level3','level2','UNKNOWN','level1'] 

I want to arrange this numpy array such that 'UNKNOWN' is always the first string in the array.

Desired array:

index1 = ['UNKNOWN','level4','level3','level2','level1']

Sorting of level is not desirable

4
  • You also sort the levels. Is this desired? Commented Jan 21, 2019 at 7:44
  • No, sorting of level is not desirable. Commented Jan 21, 2019 at 7:46
  • Do you want to sort lists or numpy arrays? The code you show in the question creates a list. Commented Jan 21, 2019 at 8:05
  • Does stackoverflow.com/a/37487543/3005167 or stackoverflow.com/a/45799487/3005167 answer your question? Commented Jan 21, 2019 at 8:09

3 Answers 3

1

Simplest answer

If you literally just want 'UNKNOWN' to be in front, then the easiest and most computationally efficient way to do so is to just swap it with the 0th element:

index1 = np.array(['level4','level3','level2','UNKNOWN','level1'])
# find the index of 'UNKNOWN'
ix = np.flatnonzero(index1 == 'UNKNOWN')[0]
# swap values
index1[[0, ix]] = index1[[ix, 0]]

print(index1)
# output:
#    ['UNKNOWN' 'level3' 'level2' 'level4' 'level1']

Preserve the original order of the level elements

If you want the level elements to have the same order in the output as in the input, here's a (slightly more complicated but still pretty simple) way to make that happen:

index1 = np.array(['level4','level3','level2','UNKNOWN','level1'])
# find the index of 'UNKNOWN'
ix = np.flatnonzero(index1 == 'UNKNOWN')[0]
# shift values
u = index1[ix]
index1[1:ix + 1] = index1[:ix]
index1[0] = u

print(index1)
# output:
#    ['UNKNOWN' 'level4' 'level3' 'level2' 'level1']

Don't use partition

If you don't want the results to be sorted, then you shouldn't use numpy.partition:

index1 = np.array(['level4','level3','level2','UNKNOWN','level1'])
index1.partition(np.flatnonzero(index1 == 'UNKNOWN'))

print(index1)
# output:
#     ['UNKNOWN' 'level1' 'level2' 'level3' 'level4']
Sign up to request clarification or add additional context in comments.

Comments

0

You can use np.roll:

import numpy as np

index1 = np.array(['level4','level3','level2','UNKNOWN','level1'])

position = np.where(index1 == 'UNKNOWN')[0][0]
index1[:position + 1] = np.roll(index1[:position + 1], 1)
print(index1)

array(['UNKNOWN', 'level4', 'level3', 'level2', 'level1'], dtype='<U7')

2 Comments

This does not preserve the order of the other elements
@anishtain4 What exactly do you mean? The original order of the level elements was (4, 3, 2, 1), which is the same order that the level elements have in Tomothy's output.
0

partition() will permit to reorder your array, but will be sorted.

index1.partition(np.where(index1=='UNKNOWN')[0][0])

2 Comments

Please explain your answer.
Took me a second, but then I realized that the use of partition doesn't answer the question. The result will be sorted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.