Python Pandas IntervalIndex - Get integer location for requested label

PythonServer Side ProgrammingProgramming

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
raja
Updated on 18-Oct-2021 07:57:32

Advertisements