পান্ডাসে pandas.CategoricalIndex() মেথড ব্যবহার করে একটি ক্যাটাগরিক্যাল ভিত্তিক ইনডেক্স তৈরি করা যায়। এটি সীমিত সংখ্যক নির্দিষ্ট মান ধারণ করে।
প্রয়োজনীয় লাইব্রেরি ইম্পোর্ট
import pandas as pd
ক্যাটাগরিক্যাল ইনডেক্স তৈরি
catIndex = pd.CategoricalIndex(
["p", "q", "r", "s", "p", "q", "r", "s"],
ordered=True,
categories=["p", "q", "r", "s"]
)
ইনডেক্স ও ক্যাটাগরি প্রদর্শন
print("Categorical Index...\n", catIndex)
print("\nDisplaying Categories from CategoricalIndex...\n", catIndex.categories)
সম্পূর্ণ উদাহরণ কোড (min, max সহ)
import pandas as pd
catIndex = pd.CategoricalIndex(
["p", "q", "r", "s", "p", "q", "r", "s"],
ordered=True,
categories=["p", "q", "r", "s"]
)
print("Categorical Index...\n", catIndex)
print("\nDisplaying Categories from CategoricalIndex...\n", catIndex.categories)
print("\nMinimum value from CategoricalIndex...\n", catIndex.min())
print("\nMaximum value from CategoricalIndex...\n", catIndex.max())
আউটপুট
Categorical Index...
CategoricalIndex(['p', 'q', 'r', 's', 'p', 'q', 'r', 's'],
categories=['p', 'q', 'r', 's'],
ordered=True,
dtype='category')
Displaying Categories from CategoricalIndex...
Index(['p', 'q', 'r', 's'], dtype='object')
Minimum value from CategoricalIndex...
p
Maximum value from CategoricalIndex...
s
ব্যাখ্যা
categories: ক্যাটাগরির অর্ডার নির্ধারণ করে।ordered=True: ক্যাটাগরিগুলো সর্টেড মনে করে min/max কাজ করে।- মেমরি দক্ষ: ক্যাটাগরিক্যাল ডেটা ইন্টিজার কোড হিসেবে স্টোর হয়।