หากต้องการทราบตำแหน่งของช่วงเวลาที่เกี่ยวข้องทั้งหมดหากป้ายกำกับอยู่หลายช่วง ให้ใช้ get_loc() วิธีการในนุ่น
ขั้นแรก นำเข้าไลบรารีที่จำเป็น -
import pandas as pd
สร้างวัตถุช่วงเวลาสองรายการ ช่วงเวลาที่ปิดถูกตั้งค่าโดยใช้พารามิเตอร์ "closed" โดยมีค่าเป็น "both"
interval1 = pd.Interval(50, 75) interval2 = pd.Interval(75, 90) interval3 = pd.Interval(50, 90)
สร้าง IntervalIndex จากสามช่วง -
index = pd.IntervalIndex([interval1, interval2, interval3])
รับตำแหน่งของช่วงที่เกี่ยวข้องทั้งหมดหากป้ายกำกับอยู่หลายช่วง -
print("\nGet the locations of all the relevant interval...\n",index.get_loc(65)) ตัวอย่าง
ต่อไปนี้เป็นรหัส -
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)) ผลลัพธ์
สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -
Interval1... (50, 75] Interval2... (75, 90] Interval3... (50, 90] Get the locations of all the relevant interval... [ True False True]