Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How np.argsort works in pandas DataFrame?

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.

like image 576
Karthik Elangovan Avatar asked Feb 07 '23 06:02

Karthik Elangovan


1 Answers

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
    • this was 1.0
  • 4 refers to the item in the 4 position (5th) was next
    • also 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 maximum

It'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
like image 117
piRSquared Avatar answered Feb 20 '23 10:02

piRSquared