কম্পিউটার

Python Pandas CategoricalIndex - বিভাগগুলি পুনরায় সাজান


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

import pandas as pd

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

ব্যবহার করে শ্রেণীবদ্ধকে ক্রমানুসারে বিবেচনা করুন
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("\nDisplayingCategories from CategoricalIndex...\n",catIndex.categories)

reorder_categories() ব্যবহার করে বিভাগগুলি পুনরায় সাজান। বিভাগগুলিকে একটি প্যারামিটার হিসাবে নতুন ক্রমে সেট করুন −

print("\nCategoricalIndex after reordering categories...\n",catIndex.reorder_categories(["r", "s", "q", "p"]))

উদাহরণ

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

import pandas as pd

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

# Reorder categories using reorder_categories()
# Set the categories in new order as a parameter
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')

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

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

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

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

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