ইনডেক্স মান সর্ট করতে এবং সর্টিং-ইন্ডেক্স (যে পজিশনগুলো থেকে এসেছে) পেতে index.sort_values(return_indexer=True) ব্যবহার করুন। এটি একটি টাপল রিটার্ন করে: (সোর্টেড ইনডেক্স, সর্টিং ইন্ডেক্সার অ্যারের)।
প্রয়োজনীয় লাইব্রেরি ইমপোর্ট করুন
import pandas as pd
প্যান্ডাস ইনডেক্স তৈরি করুন
index = pd.Index([50, 10, 70, 95, 110, 90, 30])
ইনডেক্স প্রদর্শন করুন
print("Pandas Index...\n", index)
সর্ট করুন এবং ইন্ডেক্সার রিটার্ন করুন
print("\nSort and also return the indices that would sort the index...\n", index.sort_values(return_indexer=True))
সম্পূর্ণ উদাহরণ
import pandas as pd
# Creating Pandas index
index = pd.Index([50, 10, 70, 95, 110, 90, 30])
# 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)
# Sort index values
# By default, sorts in Ascending order
# Return the indices to sort the index using the "return_indexer" parameter with value True
print("\nSort and also return the indices that would sort the index...\n", index.sort_values(return_indexer=True))
আউটপুট
Pandas Index... Int64Index([50, 10, 70, 95, 110, 90, 30], dtype='int64')
Number of elements in the index... 7
Sort and also return the indices that would sort the index...
(Int64Index([10, 30, 50, 70, 90, 95, 110], dtype='int64'), array([1, 6, 0, 2, 5, 3, 4], dtype=int64))
ব্যাখ্যা: সর্টেড ইনডেক্স: [10, 30, 50, 70, 90, 95, 110]। ইন্ডেক্সার [1, 6, 0, 2, 5, 3, 4] মানে: সর্টেড ইনডেক্সের ০তম উপাদান (১০) মূল ইনডেক্সের ১ নম্বর পজিশন থেকে এসেছে, ১ম উপাদান (৩০) ৬ নম্বর পজিশন থেকে, ইত্যাদি।