इनपुट लेबल के लिए स्लाइस इंडेक्सर की गणना करने के लिए, index.slice_indexer() विधि का उपयोग करें। सबसे पहले, आवश्यक लाइब्रेरी आयात करें:
import pandas as pd
पैंडास इंडेक्स ऑब्जेक्ट बनाएं:
index = pd.Index(list('pqrstuvwxyz'))
पैंडास इंडेक्स प्रदर्शित करें:
print("Pandas Index...\n", index)
स्लाइस इंडेक्सर प्राप्त करें। "start" वह लेबल है जिससे शुरू करना है। "end" वह लेबल है जहां समाप्त करना है:
print("\nThe slice indexer with start and stop...\n", index.slice_indexer(start='s', end='w'))
उदाहरण
निम्नलिखित कोड है:
import pandas as pd
# पैंडास इंडेक्स ऑब्जेक्ट बनाएं
index = pd.Index(list('pqrstuvwxyz'))
# पैंडास इंडेक्स प्रदर्शित करें
print("Pandas Index...\n", index)
# इंडेक्स में तत्वों की संख्या लौटाएं
print("\nNumber of elements in the index...\n", index.size)
# स्लाइस इंडेक्सर प्राप्त करें
# "start" वह लेबल है जिससे शुरू करना है
# "end" वह लेबल है जहां समाप्त करना है
print("\nThe slice indexer with start and stop...\n", index.slice_indexer(start='s', end='w'))
आउटपुट
यह निम्नलिखित आउटपुट उत्पन्न करेगा:
Pandas Index... Index(['p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'], dtype='object') Number of elements in the index... 11 The slice indexer with start and stop... slice(3, 8, None)