I have the following pandas DataFrame named index
:
tz
521.0
Africa/Cairo 3.0
Africa/Casablanca 1.0
Africa/Ceuta 2.0
Africa/Johannesburg 1.0
dtype: float64
when I apply index.argsort()
I get something like this:
tz
2
Africa/Cairo 4
Africa/Casablanca 3
Africa/Ceuta 1
Africa/Johannesburg 0
dtype: int64
Can someone explain to me where the numbers: 2,4,3,1,0
come? I know they're index range from 0 to 4 but I can't find any logic in their order.
argsort
returns the index positions of the values being sorted if they were to be sorted. Keep in mind that this is a numpy function and its assignment to series or dataframe indices is erroneous.
2
refers to the item in the 2
position (3rd) was the minimum
1.0
4
refers to the item in the 4
position (5th) was next
1.0
3
(4th position) was a 2.0
1
(2nd position) was a 3.0
0
(1st position) was a 521.0
and the maximumIt's more appropriate to assign to an array and use as a slice
a = s.values.argsort()
s.iloc[a]
tz
Africa/Casablanca 1.0
Africa/Johannesburg 1.0
Africa/Ceuta 2.0
Africa/Cairo 3.0
521.0
Name: value, dtype: float64
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With