

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 slice locations for input labels
To compute slice locations for input labels, use the index.slice_locs() 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 locations. The "start" is the label to begin with. The "end" is the label to end with
print("\nThe slice locations with start and stop...\n",index.slice_locs(start='q', end='v'))
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 locations # The "start" is the label to begin with # The "end" is the label to end with print("\nThe slice locations with start and stop...\n",index.slice_locs(start='q', end='v'))
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 locations with start and stop... (1, 7)
- Related Questions & Answers
- Python Pandas - Compute the slice indexer for input labels
- Compute the Natural logarithm for complex-valued input in Python
- Python Pandas - Get location for a sequence of labels in a MultiIndex
- Python Pandas - Compute indexer and mask for new index even for non-uniquely valued objects
- Python Pandas - Compute indexer and mask for new index given the current index
- Python Pandas - Get location for a label or a tuple of labels in a MultiIndex
- Python Pandas - Calculate the right slice bound that corresponds to given label
- Python Pandas - Calculate the left slice bound that corresponds to given label
- Compute the square root of input with emath in Python
- Python Pandas - Return index locations of values at particular time of day in DateTimeIndex
- Python Pandas - Return index locations of values between particular time of day in DateTimeIndex
- Python Pandas - Make new Index with passed list of labels deleted
- How can we detect duplicate labels using the Python Pandas library?
- Python - Compute last of group values in a Pandas DataFrame
- Python Pandas - Compute the symmetric difference of two Index objects
Advertisements