python Pandas - 将 IntervalArray 中每个 Interval 的左端点作为索引返回
要将IntervalArray中每个Interval的左端点作为索引返回,请使用array.left属性。
首先,导入所需的库-
import pandas as pd
创建两个Interval对象-
nterval1 = pd.Interval(10, 25) interval2 = pd.Interval(15, 70)
显示间隔-
print("Interval1...\n",interval1) print("Interval2...\n",interval2)
从Interval对象构造一个新的IntervalArray-
array = pd.arrays.IntervalArray([interval1,interval2])
获取左端点-
print("\nThe left endpoints of each Interval in the IntervalArray as an Index...\n", array.left)
示例
以下是代码-
import pandas as pd #创建两个Interval对象 interval1 = pd.Interval(10, 25) interval2 = pd.Interval(15, 70) #显示间隔 print("Interval1...\n",interval1) print("Interval2...\n",interval2) #从Interval对象构造一个新的IntervalArray array = pd.arrays.IntervalArray([interval1,interval2]) #显示间隔数组 print("\nOur IntervalArray...\n",array) #获取IntervalArray的长度 #返回一个索引,其中的条目表示IntervalArray中每个Interval的长度 print("\nOur IntervalArray length...\n",array.length) #获取左端点 print("\nThe left endpoints of each Interval in the IntervalArray as an Index...\n", array.left)输出结果
这将产生以下代码-
Interval1... (10, 25] Interval2... (15, 70] Our IntervalArray... <IntervalArray> [(10, 25], (15, 70]] Length: 2, dtype: interval[int64, right] Our IntervalArray length... Int64Index([15, 55], dtype='int64') The left endpoints of each Interval in the IntervalArray as an Index... Int64Index([10, 15], dtype='int64')