पैंडास इंडेक्स में क्रम बनाए रखने के लिए एलिमेंट्स को इन्सर्ट करने के इंडेक्स खोजने के लिए, index.searchsorted() मेथड का उपयोग करें।
सबसे पहले, आवश्यक लाइब्रेरी इंपोर्ट करें:
import pandas as pd
पैंडास इंडेक्स बनाएं:
index = pd.Index([10, 20, 30, 40, 50])
पैंडास इंडेक्स प्रदर्शित करें:
print("Pandas Index...\n", index)
searchsorted: इन्सर्ट करने के लिए मान सेट करें और सटीक इंडेक्स पोजीशन प्राप्त करें जहां इसे रखा जाना चाहिए:
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