TransWikia.com

How can I move a item in a numpy array?

Stack Overflow Asked by Lostsoul on December 16, 2021

I have a numpy array –

['L', 'London', 'M', 'Moscow', 'NYC', 'Paris', 'nan']

I want ‘nan’ to be first, like so:

['nan', 'L', 'London', 'M', 'Moscow', 'NYC', 'Paris']

How can I do that?

One Answer

If you want to use numpy, you can use numpy.roll:

a = np.array(['L', 'London', 'M', 'Moscow', 'NYC', 'Paris', 'nan'])

a = np.roll(a, 1)

print(a)

Prints:

['nan' 'L' 'London' 'M' 'Moscow' 'NYC' 'Paris']

Answered by Andrej Kesely on December 16, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP