কম্পিউটার

Python Pandas CategoricalIndex - বিভাগগুলির নাম পরিবর্তন করুন


বিভাগগুলির পুনঃনামকরণ করতে, CategoricalIndex rename_categories() ব্যবহার করুন পান্ডাসে পদ্ধতি। প্রথমে, প্রয়োজনীয় লাইব্রেরিগুলি আমদানি করুন -

import pandas as pd

CategoricalIndex শুধুমাত্র একটি সীমিত, এবং সাধারণত স্থির, সম্ভাব্য মানের সংখ্যা নিতে পারে। "বিভাগ" পরামিতি ব্যবহার করে শ্রেণীগত জন্য বিভাগ সেট করুন। "অর্ডার করা" পরামিতি −

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

বিভাগগুলির নাম পরিবর্তন করুন। নতুন বিভাগগুলি সেট করুন যা পুরানো বিভাগগুলিকে প্রতিস্থাপন করবে −

print("\nCategoricalIndex after renaming categories...\n",catIndex.rename_categories([5, 10, 15, 20]))

উদাহরণ

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

import pandas as pd

# CategoricalIndex can only take on a limited, and usually fixed, number of possible values
# 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)

# Rename categories
# Set the new categories that will replace the old categories
print("\nCategoricalIndex after renaming categories...\n",catIndex.rename_categories([5, 10, 15, 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 renaming categories...
CategoricalIndex([5, 10, 15, 20, 5, 10, 15, 20], categories=[5, 10, 15, 20], ordered=True, dtype='category')

  1. Python Pandas - অর্ডারকৃত ক্যাটেগরিক্যাল ইনডেক্স থেকে ন্যূনতম মান পান

  2. Python Pandas - একটি অন্তর্নিহিত শ্রেণীবিভাগের উপর ভিত্তি করে একটি সূচক তৈরি করুন

  3. পাইথন-পান্ডাসে টাইমস্ট্যাম্প তুলনা করা

  4. পাইথন - পান্ডাস ডেটাফ্রেম.রিনেম()