কম্পিউটার

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


একটি অন্তর্নিহিত ক্যাটেগরিক্যালের উপর ভিত্তি করে একটি সূচক তৈরি করতে, pandas.CategoricalIndex() ব্যবহার করুন পদ্ধতি।

প্রথমে, প্রয়োজনীয় লাইব্রেরিগুলি আমদানি করুন -

import pandas as pd

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

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

শ্রেণীগত সূচক প্রদর্শন করুন -

print("Categorical Index...\n",catIndex)

বিভাগগুলি পান
print("\nDisplayingCategories from CategoricalIndex...\n",catIndex.categories)

উদাহরণ

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

import pandas as pd

# CategoricalIndex is the Index based on an underlying Categorical
# 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 Categorical Index
print("Categorical Index...\n",catIndex)

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

# Get the min value
print("\nMinimum value from CategoricalIndex...\n",catIndex.min())

# Get the max value
print("\nMaximum value from CategoricalIndex...\n",catIndex.max())

আউটপুট

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

তৈরি করবে
Categorical Index...
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')

Minimum value from CategoricalIndex...
p

Maximum value from CategoricalIndex...
S

  1. Python Pandas - dtypes-এ কাস্ট করা মান সহ একটি সূচক তৈরি করুন

  2. Python Pandas - একটি সূচকের উপাদানগুলি পুনরাবৃত্তি করুন

  3. Python Pandas - পরিবর্তন সূচক নাম

  4. পাইথন - পান্ডাস সূচকে সুনির্দিষ্ট ডেটা আছে কিনা তা পরীক্ষা করুন