পাইথন প্যান্ডাসে CategoricalIndex.map() মেথড ব্যবহার করে ক্যাটেগরিক্যাল মানগুলোকে ডিকশনারির মতো ম্যাপিং করে নতুন মান বরাবর করা যায়।
প্রয়োজনীয় লাইব্রেরি ইম্পোর্ট করুন
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("\nCategoricalIndex after mapping...\n", catIndex.map({'P': 5, 'Q': 10, 'R': 15, 'S': 20}))
সম্পূর্ণ উদাহরণ
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 mapping...\n", catIndex.map({'P': 5, 'Q': 10, 'R': 15, 'S': 20}))
আউটপুট
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 mapping... CategoricalIndex([5, 10, 15, 20, 5, 10, 15, 20], categories=[5, 10, 15, 20], ordered=True, dtype='category')
ব্যাখ্যা: P→5, Q→10, R→15, S→20 ম্যাপিং হয়েছে। নতুন ক্যাটেগরি [5, 10, 15, 20] এবং অর্ডার বজায় থাকে।