একটি সোর্টেড প্যান্ডাস Index-এ একক মান কোথায় ইনসার্ট করলে অর্ডার বজায় থাকবে, তা জানতে index.searchsorted(value) মেথড ব্যবহার করুন।
প্রয়োজনীয় লাইব্রেরি ইমপোর্ট করুন
import pandas as pd
প্যান্ডাস ইনডেক্স তৈরি করুন (সোর্টেড হতে হবে)
index = pd.Index([10, 20, 30, 40, 50])
ইনডেক্স প্রদর্শন করুন
print("Pandas Index...\n", index)
ইনসারশন পজিশন খুঁজুন (একক মান)
print("\nThe exact positions where the element should be placed?...\n", index.searchsorted(45))
সম্পূর্ণ উদাহরণ
import pandas as pd
# Creating Pandas index
index = pd.Index([10, 20, 30, 40, 50])
# 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)
# searchsorted
# set the value to insert and get the exact index position where it should be placed
print("\nThe exact positions where the element should be placed?...\n", index.searchsorted(45))
আউটপুট
Pandas Index... Int64Index([10, 20, 30, 40, 50], dtype='int64')
Number of elements in the index... 5
The exact positions where the element should be placed?... 4
ব্যাখ্যা: ৪৫ ইনসার্ট করলে ৪ নম্বর পজিশনে (৪০ এবং ৫০-এর মাঝে) যাবে। searchsorted বাইনারি সার্চ ব্যবহার করে, তাই ইনডেক্স সোর্টেড থাকা বাধ্যতামূলক।