ইনডেক্স মান অ্যাসেন্ডিং (আরোহী) ক্রমে সর্ট করতে index.sort_values() ব্যবহার করুন। এটি ডিফল্ট আচরণ, তাই ascending=True লিখতে হয় না।
প্রয়োজনীয় লাইব্রেরি ইমপোর্ট করুন
import pandas as pd
প্যান্ডাস ইনডেক্স তৈরি করুন
index = pd.Index([50, 10, 70, 95, 110, 90, 30])
ইনডেক্স প্রদর্শন করুন
print("Pandas Index...\n", index)
অ্যাসেন্ডিং অর্ডারে সর্ট করুন
print("\nSort the index values...\n", index.sort_values())
সম্পূর্ণ উদাহরণ
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)
# Return the dtype of the data
print("\nThe dtype object...\n", index.dtype)
# Sort index values
# By default, sorts in Ascending order
print("\nSort the index values...\n", index.sort_values())
আউটপুট
Pandas Index... Int64Index([50, 10, 70, 95, 110, 90, 30], dtype='int64')
Number of elements in the index... 7
The dtype object... int64
Sort the index values... Int64Index([10, 30, 50, 70, 90, 95, 110], dtype='int64')
নোট: এটি নতুন ইনডেক্স রিটার্ন করে; মূল index অপরিবর্তিত থাকে। ইন-প্লেস সর্টিংয়ের জন্য index.sort_values(inplace=True) ব্যবহার করা যায় (প্যান্ডাস ২.০+ এ)।