ইনডেক্স সর্ট করতে কোন পজিশনগুলো ব্যবহার করতে হবে, সেটি জানতে index.argsort() মেথড ব্যবহার করুন। এটি ইন্টিজার অ্যারে রিটার্ন করে যা সর্টেড অর্ডার ইঙ্গিত করে।
প্রয়োজনীয় লাইব্রেরি ইমপোর্ট করুন
import pandas as pd
স্ট্রিং ইনডেক্স তৈরি করুন
index = pd.Index(['Electronics','Accessories','Decor', 'Books', 'Toys'], name='Products')
ইনডেক্স প্রদর্শন করুন
print("Pandas Index...\n", index)
সর্টিং ইন্ডেক্স (argsort) বের করুন
res = index.argsort()
সম্পূর্ণ উদাহরণ
import pandas as pd
# Creating Pandas index
index = pd.Index(['Electronics','Accessories','Decor', 'Books', 'Toys'], name='Products')
# Display the Pandas index
print("Pandas Index...\n", index)
# Return the number of elements in the Index
print("\nNumber of elements in the index...\n", index.size)
# Return the dtype of the data
print("\nThe dtype object...\n", index.dtype)
res = index.argsort()
# Return the integer indices that would sort the index
print("\nThe integer indices to sort the index...\n", res)
print("\nOrdered..\n", index[res])
আউটপুট
Pandas Index... Index(['Electronics', 'Accessories', 'Decor', 'Books', 'Toys'], dtype='object', name='Products')
Number of elements in the index... 5
The dtype object... object
The integer indices to sort the index... [1 3 2 0 4]
Ordered.. Index(['Accessories', 'Books', 'Decor', 'Electronics', 'Toys'], dtype='object', name='Products')
ব্যাখ্যা: argsort() রিটার্ন করে [1, 3, 2, 0, 4]। অর্থাৎ সর্টেড ইনডেক্সের ০তম উপাদান 'Accessories' মূল ইনডেক্সের ১ নম্বর পজিশনে ছিল, ১ম উপাদান 'Books' ৩ নম্বর পজিশনে ছিল, ইত্যাদি। index[res] দিয়ে সর্টেড ইনডেক্স পাওয়া যায়।