Python Pandas IntervalIndex - 获取请求标签的整数位置
要获取请求标签的整数位置,请使用get_loc()Pandas中的方法。首先,导入所需的库-
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 #创建两个Interval对象 # Closed intervals set using the "closed" 带值的参数 "both" interval1 = pd.Interval(50, 75) interval2 = pd.Interval(75, 90) #显示间隔 print("Interval1...\n",interval1) print("Interval2...\n",interval2) #从两个区间创建IntervalIndex index = pd.IntervalIndex([interval1, interval2]) #获取请求标签的整数位置 print("\nInteger location for requested label...\n",index.get_loc(75))输出结果
这将产生以下输出-
Interval1... (50, 75] Interval2... (75, 90] Integer location for requested label... 0