কম্পিউটার

Python Pandas CategoricalIndex - একটি ডিক্টের মতো ইনপুট চিঠিপত্র ব্যবহার করে মানচিত্র মান


একটি dict মত ইনপুট চিঠিপত্র ব্যবহার করে মান ম্যাপ করতে, CategoricalIndex.map() ব্যবহার করুন পান্ডাসে পদ্ধতি। প্রথমে, প্রয়োজনীয় লাইব্রেরিগুলি আমদানি করুন -

import pandas as pd

"বিভাগগুলি" প্যারামিটার ব্যবহার করে শ্রেণীবদ্ধের জন্য বিভাগগুলি সেট করুন। "অর্ডার করা" পরামিতি −

ব্যবহার করে শ্রেণীবদ্ধকে ক্রমানুসারে বিবেচনা করুন
catIndex = pd.CategoricalIndex(["P", "Q", "R", "S","P", "Q", "R", "S"], ordered=True, categories=["P", "Q", "R", "S"])

Categorical Index −

প্রদর্শন করুন
print("CategoricalIndex...\n",catIndex)

মানচিত্র বিভাগ −

print("\nCategoricalIndex after mapping...\n",catIndex.map({'P': 5, 'Q': 10, 'R': 15, 'S': 20}))

উদাহরণ

নিম্নলিখিত কোড -

import pandas as pd

# Set the categories for the categorical using the "categories" parameter
# Treat the categorical as ordered using the "ordered" parameter
catIndex = pd.CategoricalIndex(["P", "Q", "R", "S","P", "Q", "R", "S"], ordered=True, categories=["P", "Q", "R", "S"])

# Display the CategoricalIndex
print("CategoricalIndex...\n",catIndex)

# Get the categories
print("\nDisplayingCategories from CategoricalIndex...\n",catIndex.categories)

# Map 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')

DisplayingCategories 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')

  1. পাইথন পান্ডাস - notnull() ব্যবহার করে নাল মান পরীক্ষা করুন

  2. Python Pandas - একটি ইন্টারপোলেশন পদ্ধতি ব্যবহার করে NaN মান পূরণ করুন

  3. ম্যাপ() ফাংশন ব্যবহার করে পাইথনে যোগফল 2D অ্যারে

  4. পাইথন পান্ডা ব্যবহার করে ডেটা বিশ্লেষণ