Python | Pandas Index.argsort ()
Python - отличный язык для анализа данных, в первую очередь из-за фантастической экосистемы пакетов Python, ориентированных на данные. Pandas - один из таких пакетов, который значительно упрощает импорт и анализ данных.
Pandas Index.argsort() function returns the integer indices that would sort the index. By default the sorting order has been set to increasing order.
Syntax: Index.argsort(*args, **kwargs)
Parameters :
*args : Passed to numpy.ndarray.argsort
**kwargs : Passed to numpy.ndarray.argsortReturns : numpy.ndarray
Integer indices that would sort the index if used as an indexer
Example #1: Use Index.argsort() function to find the order of indices which would sort the given Index.
# importing pandas as pdimport pandas as pd # Creating the Indexdf = pd.Index([17, 69, 33, 5, 10, 74, 10, 5]) # Print the Indexdf |
Выход :
Let’s find the ordering of the indices which would sort the Index.
# to find the ordering of indices # that would sort the df Indexdf.argsort() |
Output :
As we can see in the output, the function has returned the ordering of the indices which would sort the given Index. We can verify that by printing the Index based on the ordering.
# Printing the Index based on the# result of the argsort() functiondf[df.argsort()] |
Output :
As we can see in the output, it is printed in a sorted order.
Example #2: Use Index.argsort() function to find the order of indices which would sort the given Index.
# importing pandas as pdimport pandas as pd # Creating the Indexdf = pd.Index(["Sam", "Alex", "Olivia", "Dan", "Brook", "Katherine"]) # Print the Indexdf |
Выход :
Let’s find the ordering of the indices which would sort the Index.
# to find the ordering of indices# that would sort the df Indexdf.argsort() |
Output :
As we can see in the output, the function has returned the ordering of the indices which would sort the given Index. We can verify that by printing the Index based on the ordering.
# Printing the Index based on the # result of the argsort() functiondf[df.argsort()] |
Выход :
Как мы видим в выводе, он печатается в отсортированном порядке.
Внимание компьютерщик! Укрепите свои основы с помощью базового курса программирования Python и изучите основы.
Для начала подготовьтесь к собеседованию. Расширьте свои концепции структур данных с помощью курса Python DS. А чтобы начать свое путешествие по машинному обучению, присоединяйтесь к курсу Машинное обучение - базовый уровень.