how np.argsort works in pandas data frame
<p>i have pandas dataframe named "index" like</p>

<pre><code>tz
                       521.0
Africa/Cairo             3.0
Africa/Casablanca        1.0   
Africa/Ceuta             2.0
Africa/Johannesburg      1.0
dtype: float64 
</code></pre>

<p>when i applied <code>index.argsort()</code> i get something like this</p>

<pre><code>tz
                       2
Africa/Cairo           4
Africa/Casablanca      3
Africa/Ceuta           1
Africa/Johannesburg    0
dtype: int64
</code></pre>

<p>can someone explain how the number "2,4,3,1,0" came? i know there are index range from 0 to 4 but i can't think of a logic in there order.</p>

<p><code>argsort</code> 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.</p>

<ul>
<li><code>2</code> refers to the item in the <code>2</code> position (3rd) was the minimum

<ul>
<li>this was <code>1.0</code> </li>
</ul></li>
<li><code>4</code> refers to the item in the <code>4</code> position (5th) was next

<ul>
<li>also <code>1.0</code></li>
</ul></li>
<li><code>3</code> (4th position) was a <code>2.0</code></li>
<li><code>1</code> (2nd position) was a <code>3.0</code></li>
<li><code>0</code> (1st position) was a <code>521.0</code> and the maximum</li>
</ul>

<hr>

<p>It's more appropriate to assign to an array and use as a slice</p>

<pre><code>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
</code></pre>

python python-3.x pandas numpy