- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How does pandas series argsort work?
The argsort() is one of the methods of the pandas series constructor, it works similar to the NumPy.ndarray.argsort(). In pandas series, the argmax() method will return a series object with the indices that would sort the original series values.
It returns a series with values replaced by sorted order of indices. And it won’t change the original series position index labels, it only replaces the values by order of sorted values positions. The argsort method tells you where that element comes from the original series object.
Example 1
import pandas as pd # creating series s = pd.Series({'A':123,'B':458,"C":556, "D": 238}) print(s) # apply argsort() print("Output argsort:",s.argsort())
Explanation
In this following example, we have created a series using a python dictionary and the index will be labeled by keys of the dictionary. Then we applied the argsort() method over the series data.
Output
A 123 B 458 C 556 D 238 dtype: int64 Output argsort: A 0 B 3 C 1 D 2 dtype: int64
In the above output block, we can see the output series of the argsort method, and you can observe the index and values of that series object if you see the element at label “B” is coming from the “3rd” index of the original series object. In the same way, the element at label “C” is coming from the 1st index of the original series object, which is the second smallest number from the original series element.
Example 2
import pandas as pd # creating series series = pd.Series([3,5,2,7]) print(series) # apply argsort() print("Output argsort:", series.argsort())
Explanation
Let’s take another example of a pandas series object to apply the argsort method. Initially, we have created a pandas series object with a list of integer values and then applied the argsort method on that data.
Output
0 3 1 5 2 2 3 7 dtype: int64 Output argsort: 0 2 1 0 2 1 3 3 dtype: int64
The element at index position 0 of the output series object is coming from the 2nd index position of the original series and it is the smallest number. In the same way, the element at index position 1 of the output series object is coming from the 0th index position of the original series and it is the second smallest number from the original series values.
- Related Articles
- How does pandas series argsort handles nan values?
- How does pandas series astype() method work?
- How does pandas series combine() method work?
- How does pandas series combine_first() method work?
- How does pandas series div() method work?
- How does the pandas Series idxmax() method work?
- How does the pandas Series idxmin() method work?
- How to leave nan as nan in the pandas series argsort method?
- How does the pandas series.gt() method work if the series object contains string-type elements?
- How does the pandas series.ffill() method work?
- How does the pandas series.expanding() method work?
- How does the pandas series.first_valid_index() method work?
- How does the series.copy() method work in Pandas?
- How does the series.corr() method work in pandas?
- How does the series.cummax() method work in Pandas?
