要从类似数组的元组构造 IntervalArray,请使用pandas.arrays。IntervalArray.from_tuples()方法。要返回 IntervalArray 中每个 Interval 的正确端点,请使用array.right属性。
首先,导入所需的库 -
import pandas as pd
从类似数组的元组构造一个新的 IntervalArray -
array = pd.arrays.IntervalArray.from_tuples([(10, 25),(15, 70)])
显示间隔 -
print("Our IntervalArray...\n",array)
获得正确的端点 -
print("\nThe right endpoints of each Interval in the IntervalArray as an Index...\n", array.right)
以下是代码 -
import pandas as pd # 从类似数组的元组构造一个新的 IntervalArray array = pd.arrays.IntervalArray.from_tuples([(10, 25),(15, 70)]) # 显示间隔数组 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)输出结果
这将产生以下代码 -
Our IntervalArray... <IntervalArray> [(10, 25], (15, 70]] Length: 2, dtype: interval[int64, right] Our IntervalArray length... Int64Index([15, 55], dtype='int64') The midpoint of each interval in the IntervalArray... Float64Index([17.5, 42.5], dtype='float64') The right endpoints of each Interval in the IntervalArray as an Index... Int64Index([25, 70], dtype='int64')