Python Pandas IntervalIndex - Get integer location for requested label


To get integer location for requested label, 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)

Create IntervalIndex from the two intervals −

index = pd.IntervalIndex([interval1, interval2])

Get integer location for requested label −

print("\nInteger location for requested label...\n",index.get_loc(75))

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)

# display the intervals
print("Interval1...\n",interval1)
print("Interval2...\n",interval2)

# Create IntervalIndex from the two intervals
index = pd.IntervalIndex([interval1, interval2])

# Get integer location for requested label
print("\nInteger location for requested label...\n",index.get_loc(75))

Output

This will produce the following output −

Interval1...
(50, 75]
Interval2...
(75, 90]

Integer location for requested label...
0

Updated on: 18-Oct-2021

126 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements