要按元素检查 Intervals 是否包含该值,请使用该方法。array.contains()
首先,导入所需的库 -
import pandas as pd
从类似数组的拆分构造一个新的 IntervalArray -
array = pd.arrays.IntervalArray.from_breaks([0, 1, 2, 3, 4, 5])
显示间隔 -
print("Our IntervalArray...\n",array)
检查间隔是否包含特定值 -
print("\nDoes the Intervals contain the value? \n",array.contains(3.5))
以下是代码 -
import pandas as pd # 从类似数组的拆分构造一个新的 IntervalArray array = pd.arrays.IntervalArray.from_breaks([0, 1, 2, 3, 4, 5]) # 显示间隔数组 print("Our IntervalArray...\n",array) # 获取 IntervalArray 的长度 # 返回一个索引,其中的条目表示 IntervalArray 中每个 Interval 的长度 print("\nOur IntervalArray length...\n",array.length) # IntervalArray 中每个 Interval 的中点作为索引 print("\nThe midpoint of each interval in the IntervalArray...\n",array.mid) # 获得正确的端点 print("\nThe right endpoints of each Interval in the IntervalArray as an Index...\n",array.right) print("\nDoes the Intervals contain the value? \n",array.contains(3.5))输出结果
这将产生以下代码 -
Our IntervalArray... <IntervalArray> [(0, 1], (1, 2], (2, 3], (3, 4], (4, 5]] Length: 5, dtype: interval[int64, right] Our IntervalArray length... Int64Index([1, 1, 1, 1, 1], dtype='int64') The midpoint of each interval in the IntervalArray... Float64Index([0.5, 1.5, 2.5, 3.5, 4.5], dtype='float64') The right endpoints of each Interval in the IntervalArray as an Index... Int64Index([1, 2, 3, 4, 5], dtype='int64') Does the Intervals contain the value? [False False False True False]