কম্পিউটার

Python Pandas - সূচী মান প্রতিস্থাপন করুন যেখানে শর্তটি মিথ্যা


সূচক মান প্রতিস্থাপন করতে যেখানে শর্তটি মিথ্যা, ব্যবহার করুন index.isin() পান্ডাসে পদ্ধতি। প্রথমে, প্রয়োজনীয় লাইব্রেরিগুলি আমদানি করুন -

import pandas as pd

পান্ডাস সূচক তৈরি করা হচ্ছে -

index = pd.Index(['Electronics','Accessories','Decor', 'Books', 'Toys'], name ='Products')

পান্ডাস সূচক প্রদর্শন করুন -

print("Pandas Index...\n",index)

মান প্রতিস্থাপন করুন যেখানে শর্ত False হয়। এখানে, 'সজ্জা' বাদে, অন্য প্রতিটি উপাদান প্রতিস্থাপিত হয় -

print("\nReplace index vales where condition is False...\n",index.where(index.isin(['Decor']), 'Miscellaneous'))

উদাহরণ

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

import pandas as pd

# Creating Pandas index
index = pd.Index(['Electronics','Accessories','Decor', 'Books', 'Toys'], name ='Products')

# Display the Pandas index
print("Pandas Index...\n",index)

# Return the number of elements in the Index
print("\nNumber of elements in the index...\n",index.size)

# Return the dtype of the data
print("\nThe dtype object...\n",index.dtype)

# replace values where condition is False
# Here, except the 'Decor', every other element gets replaced
print("\nReplace index vales where condition is False...\n",index.where(index.isin(['Decor']), 'Miscellaneous'))

আউটপুট

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

তৈরি করবে
Pandas Index...
Index(['Electronics', 'Accessories', 'Decor', 'Books', 'Toys'], dtype='object', name='Products')

Number of elements in the index...
5

The dtype object...
object

Replace index vales where condition is False...
Index(['Miscellaneous', 'Miscellaneous', 'Decor', 'Miscellaneous','Miscellaneous'],dtype='object', name='Products')

  1. Python Pandas - মাস্কের সাথে সেট করা মানগুলির একটি নতুন সূচক ফেরত দিন

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

  3. Python - পান্ডাস সূচকের সর্বোচ্চ মান ফেরত দিন

  4. পাইথন - একটি ডেটাফ্রেমের মান পান্ডাসে অন্য ডেটাফ্রেমের মান দিয়ে প্রতিস্থাপন করুন