
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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)
- Related Articles
- Python Pandas - Compute slice locations for input labels
- Python Pandas - Compute indexer and mask for new index given the current index
- Python Pandas - Compute indexer and mask for new index even for non-uniquely valued objects
- Python Pandas - Compute indexer and find the previous index value if no exact match
- Python Pandas - Compute indexer and find the next index value if no exact match
- Python Pandas - Compute indexer and find the nearest index value if no exact match
- Compute the Natural logarithm for complex-valued input in Python
- Python Pandas - Get location for a sequence of labels in a MultiIndex
- How to apply the slicing indexer to the pandas DataFrame.iloc attribute?
- How to apply the slicing indexer to the pandas DataFrame.loc attribute?
- Compute the square root of input with emath in Python
- How can we detect duplicate labels using the Python Pandas library?
- Python Pandas - Compute the symmetric difference of two Index objects
- Compute the square root of negative input with emath in Python
- Python Pandas - Get location for a label or a tuple of labels in a MultiIndex

Advertisements