কম্পিউটার টিউটোরিয়াল

পাইথন প্যান্ডাস - CategoricalIndex-কে অর্ডার্ড (क्रमবদ্ধ) করুন

পাইথন প্যান্ডাসে as_ordered() মেথড ব্যবহার করে CategoricalIndex-কে অর্ডার্ড (क्रमবদ্ধ) করা যায়। অর্ডার্ড ক্যাটেগরিক্যালে বড়-ছোট তুলনা করা যায়।

প্রয়োজনীয় লাইব্রেরি ইম্পোর্ট করুন

import pandas as pd

CategoricalIndex তৈরি করুন (অনঅর্ডার্ড)

ক্যাটেগরি: ['p', 'q', 'r', 's']

catIndex = pd.CategoricalIndex(["p", "q", "r", "s", "p", "q", "r", "s"], categories=["p", "q", "r", "s"])

ক্যাটেগরি প্রদর্শন করুন

print("\nDisplaying Categories from CategoricalIndex...\n", catIndex.categories)

অর্ডার্ড করুন

print("\nCategoricalIndex ordered...\n", catIndex.as_ordered())

সম্পূর্ণ উদাহরণ

import pandas as pd

# CategoricalIndex তৈরি (অনঅর্ডার্ড)
catIndex = pd.CategoricalIndex(["p", "q", "r", "s", "p", "q", "r", "s"], categories=["p", "q", "r", "s"])

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

# ক্যাটেগরি প্রদর্শন
print("\nDisplaying Categories from CategoricalIndex...\n", catIndex.categories)

# অর্ডার্ড করুন
print("\nCategoricalIndex ordered...\n", catIndex.as_ordered())

আউটপুট

CategoricalIndex... CategoricalIndex(['p', 'q', 'r', 's', 'p', 'q', 'r', 's'], categories=['p', 'q', 'r', 's'], ordered=False, dtype='category')
Displaying Categories from CategoricalIndex... Index(['p', 'q', 'r', 's'], dtype='object')
CategoricalIndex ordered... CategoricalIndex(['p', 'q', 'r', 's', 'p', 'q', 'r', 's'], categories=['p', 'q', 'r', 's'], ordered=True, dtype='category')

ব্যাখ্যা: মূলত ordered=False ছিল, as_ordered() কল করার পর ordered=True হয়েছে।

  1. পান্ডাসে notnull() দিয়ে নাল মান চেক করা

  2. পান্ডাসে interpolate() দিয়ে NaN মান পূরণ করা

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

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