পাইথন প্যান্ডাসে rename_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)
ল্যাম্বডা দিয়ে ক্যাটেগরি রিনেম করুন
সব ক্যাটেগরি লোয়ারকেসে রূপান্তর: lambda a: a.lower()
print("\nCategoricalIndex after renaming categories...\n", catIndex.rename_categories(lambda a: a.lower()))
সম্পূর্ণ উদাহরণ
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 renaming categories...\n", catIndex.rename_categories(lambda a: a.lower()))
আউটপুট
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 renaming categories... CategoricalIndex(['p', 'q', 'r', 's', 'p', 'q', 'r', 's'], categories=['p', 'q', 'r', 's'], ordered=True, dtype='category')
ব্যাখ্যা: ল্যাম্বডা ফাংশন lambda a: a.lower() প্রতিটি ক্যাটেগরিকে লোয়ারকেসে রূপান্তর করে। P→p, Q→q, R→r, S→s।