Python Pandas IntervalIndex - 获取请求标签的整型位置


若要获取请求标签的整型位置,请使用 Pandas 中的 get_loc() 方法。首先,导入所需的库 −

import pandas as pd

创建两个 Interval 对象。使用值 "both" 的 "closed" 参数设置闭区间 −

interval1 = pd.Interval(50, 75)
interval2 = pd.Interval(75, 90)

通过这两个区间创建 IntervalIndex −

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

获取请求标签的整型位置 −

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

示例

以下为代码 −

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))

输出

将生成以下输出 −

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

Integer location for requested label...
0

更新于: 18-10-2021

152 次浏览

开启你的 职业

完成课程以获得认证

开始
广告