- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Pandas - Compute the slice indexer for input labels
To compute the slice indexer for input labels, use the index.slice_indexer() method. At first, import the required libraries −
import pandas as pd
Create Pandas index object −
index = pd.Index(list('pqrstuvwxyz'))
Display the Pandas index −
print("Pandas Index...\n",index)
Get the slice indexer. The "start" is the label to begin with. The "end" is the label to end with −
print("\nThe slice indexer with start and stop...\n",index.slice_indexer(start='s', end='w'))
Example
Following is the code −
import pandas as pd # create Pandas index object index = pd.Index(list('pqrstuvwxyz')) # 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) # Get the slice indexer # The "start" is the label to begin with # The "end" is the label to end with print("\nThe slice indexer with start and stop...\n",index.slice_indexer(start='s', end='w'))
Output
This will produce the following output −
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)
Advertisements