পাইথন প্যান্ডাসে reorder_categories() মেথড ব্যবহার করে CategoricalIndex-এ ক্যাটেগরিগুলোর ক্রম পরিবর্তন করা যায়। এটি ক্যাটেগরির অর্ডার পরিবর্তন করে কিন্তু মানগুলো অপরিবর্তিত রাখে।
প্রয়োজনীয় লাইব্রেরি ইম্পোর্ট করুন
import pandas as pd
CategoricalIndex তৈরি করুন
ক্যাটেগরি: ['p', 'q', 'r', 's'], অর্ডার্ড: True
catIndex = pd.CategoricalIndex(["p", "q", "r", "s", "p", "q", "r", "s"], ordered=True, categories=["p", "q", "r", "s"])
CategoricalIndex প্রদর্শন করুন
print("CategoricalIndex...\n", catIndex)
ক্যাটেগরি প্রদর্শন করুন
print("\nDisplaying Categories from CategoricalIndex...\n", catIndex.categories)
ক্যাটেগরি রি-অর্ডার করুন
নতুন ক্রম: ['r', 's', 'q', 'p']
print("\nCategoricalIndex after reordering categories...\n", catIndex.reorder_categories(["r", "s", "q", "p"]))
সম্পূর্ণ উদাহরণ
import pandas as pd
# CategoricalIndex তৈরি
catIndex = pd.CategoricalIndex(["p", "q", "r", "s", "p", "q", "r", "s"], ordered=True, categories=["p", "q", "r", "s"])
# CategoricalIndex প্রদর্শন
print("CategoricalIndex...\n", catIndex)
# ক্যাটেগরি প্রদর্শন
print("\nDisplaying Categories from CategoricalIndex...\n", catIndex.categories)
# ক্যাটেগরি রি-অর্ডার করুন
print("\nCategoricalIndex after reordering categories...\n", catIndex.reorder_categories(["r", "s", "q", "p"]))
আউটপুট
CategoricalIndex... 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') CategoricalIndex after reordering categories... CategoricalIndex(['p', 'q', 'r', 's', 'p', 'q', 'r', 's'], categories=['r', 's', 'q', 'p'], ordered=True, dtype='category')
ব্যাখ্যা: ক্যাটেগরির নতুন ক্রম: r, s, q, p। মানগুলো অপরিবর্তিত, শুধু ক্যাটেগরির অর্ডার পরিবর্তন হয়েছে।