কম্পিউটার

Python Pandas CategoricalIndex - নতুন বিভাগ যোগ করুন


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

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)

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

বিভাগে শেষ/সর্বোচ্চ স্থানে অন্তর্ভুক্ত করা হবে
print("\nCategoricalIndex after adding new categories...\n",catIndex.add_categories(["a", "b", "c", "d"]))

উদাহরণ

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

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)

# Add new categories using add_categories()
# Set the new categories as a parameter
# The new categories will be included at the last/highest place in the categories
print("\nCategoricalIndex after adding new categories...\n",catIndex.add_categories(["a", "b", "c", "d"]))

আউটপুট

এটি নিম্নলিখিত আউটপুট −

তৈরি করবে
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 adding new categories...
CategoricalIndex(['p', 'q', 'r', 's', 'p', 'q', 'r', 's'], categories=['p', 'q', 'r', 's', 'a', 'b', 'c', 'd'], ordered=True, dtype='category')

  1. Python - একটি পান্ডাস ডেটাফ্রেমে কলামের নামের সাথে একটি উপসর্গ যোগ করুন

  2. পাইথনে add() সেট করুন

  3. পাইথনে পান্ডাসে বিদ্যমান ডেটাফ্রেমে একটি নতুন কলাম যুক্ত করা হচ্ছে

  4. কিভাবে পাইথনে একটি অভিধানে নতুন কী যোগ করবেন?