
- 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 IntervalIndex - Get the locations of all the relevant interval if a label is in several intervals
To get the locations of all the relevant interval if a label is in several intervals, use the get_loc() method in Pandas.
At first, import the required libraries −
import pandas as pd
Create two Interval objects. Closed intervals set using the "closed" parameter with value "both"
interval1 = pd.Interval(50, 75) interval2 = pd.Interval(75, 90) interval3 = pd.Interval(50, 90)
Create IntervalIndex from the three intervals −
index = pd.IntervalIndex([interval1, interval2, interval3])
Get the locations of all the relevant interval if a label is in several intervals −
print("\nGet the locations of all the relevant interval...\n",index.get_loc(65))
Example
Following is the code −
import pandas as pd # Create two Interval objects # Closed intervals set using the "closed" parameter with value "both" interval1 = pd.Interval(50, 75) interval2 = pd.Interval(75, 90) interval3 = pd.Interval(50, 90) # display the intervals print("Interval1...\n",interval1) print("Interval2...\n",interval2) print("Interval3...\n",interval3) # Create IntervalIndex from the three intervals index = pd.IntervalIndex([interval1, interval2, interval3]) # Get the locations of all the relevant interval if a label is in several intervals print("\nGet the locations of all the relevant interval...\n",index.get_loc(65))
Output
This will produce the following output −
Interval1... (50, 75] Interval2... (75, 90] Interval3... (50, 90] Get the locations of all the relevant interval... [ True False True]
- Related Articles
- Python Pandas - Check if the IntervalIndex has overlapping intervals
- Python Pandas IntervalIndex - Get integer location for requested label
- Python Pandas - Check elementwise if the Intervals in the IntervalIndex contain the value
- Check elementwise if the Intervals in the IntervalIndex contain the value in Python Pandas
- Python Pandas IntervalIndex - Indicates if an interval is empty (contains no points)
- Python Pandas IntervalIndex - Check if Intervals that share closed endpoints overlap
- Python Pandas - Get the midpoint from the IntervalIndex
- Python Pandas - Get the length from the IntervalIndex
- Python Pandas IntervalIndex - Check if an interval that contains points is empty or not
- Python Pandas IntervalIndex - Check if an interval with missing values is empty or not
- Python Pandas - Get the left bound for the IntervalIndex
- Python Pandas - Get the right bound for the IntervalIndex
- Python Pandas - Get the length of the Interval
- Python Pandas - Check if the Intervals in the IntervalArray is empty
- Python Pandas IntervalIndex - Check if Intervals that only have an open endpoint in common overlap or not

Advertisements