পাইথন প্যান্ডাসে as_ordered() মেথড ব্যবহার করে CategoricalIndex-কে অর্ডার্ড (क्रमবদ্ধ) করা যায়। অর্ডার্ড ক্যাটেগরিক্যালে বড়-ছোট তুলনা করা যায়।
প্রয়োজনীয় লাইব্রেরি ইম্পোর্ট করুন
import pandas as pd
CategoricalIndex তৈরি করুন (অনঅর্ডার্ড)
ক্যাটেগরি: ['p', 'q', 'r', 's']
catIndex = pd.CategoricalIndex(["p", "q", "r", "s", "p", "q", "r", "s"], categories=["p", "q", "r", "s"])
ক্যাটেগরি প্রদর্শন করুন
print("\nDisplaying Categories from CategoricalIndex...\n", catIndex.categories)
অর্ডার্ড করুন
print("\nCategoricalIndex ordered...\n", catIndex.as_ordered())
সম্পূর্ণ উদাহরণ
import pandas as pd
# CategoricalIndex তৈরি (অনঅর্ডার্ড)
catIndex = pd.CategoricalIndex(["p", "q", "r", "s", "p", "q", "r", "s"], categories=["p", "q", "r", "s"])
# CategoricalIndex প্রদর্শন
print("CategoricalIndex...\n", catIndex)
# ক্যাটেগরি প্রদর্শন
print("\nDisplaying Categories from CategoricalIndex...\n", catIndex.categories)
# অর্ডার্ড করুন
print("\nCategoricalIndex ordered...\n", catIndex.as_ordered())
আউটপুট
CategoricalIndex... CategoricalIndex(['p', 'q', 'r', 's', 'p', 'q', 'r', 's'], categories=['p', 'q', 'r', 's'], ordered=False, dtype='category') Displaying Categories from CategoricalIndex... Index(['p', 'q', 'r', 's'], dtype='object') CategoricalIndex ordered... CategoricalIndex(['p', 'q', 'r', 's', 'p', 'q', 'r', 's'], categories=['p', 'q', 'r', 's'], ordered=True, dtype='category')
ব্যাখ্যা: মূলত ordered=False ছিল, as_ordered() কল করার পর ordered=True হয়েছে।