Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to sort an array function

Tags:

python

pandas

i have this code

pd.unique(df_dataset["City"])

then this output comes out

array(['Marseile', 'Barcelona', 'Valencia', 'Paris', 'Berlin', 'Lyon',
       'Seville', 'Palma', 'Munich', 'Hamburg', 'Madrid', 'Nice',
       'Granada'], dtype=object)

how do i add sort() function in the code

I have tried to run this

pd.unique(df_dataset["City"]).sorted("City", key=True)

but it doesn't seems correct

like image 519
zizamuft Avatar asked Jul 29 '26 08:07

zizamuft


2 Answers

Let us just with pandas

df_dataset["City"].sort_values().unique()
like image 157
BENY Avatar answered Jul 31 '26 23:07

BENY


What about:

sorted(df_dataset["City"].unique())

If you want to keep the numpy.array type:

import numpy as np
np.sort(df_dataset["City"].unique())
like image 25
mozway Avatar answered Jul 31 '26 23:07

mozway



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!