Python Pandas - 返回一个索引,其中包含表示 IntervalArray 中每个 Interval 长度的条目
要返回包含表示IntervalArray中每个Interval长度的条目的索引,请使用array.length属性。
首先,导入所需的库-
import pandas as pd
创建两个Interval对象。使用值为“both”的“closed”参数设置的闭合区间-
interval1 = pd.Interval(50, 75, closed='both') interval2 = pd.Interval(65, 95, closed='both')
从Interval对象构造一个新的IntervalArray-
array = pd.arrays.IntervalArray([interval1,interval2])
显示IntervalArray-
print("\nOur IntervalArray...\n",array)
获取IntervalArray的长度。返回一个索引,其中的条目表示IntervalArray中每个Interval的长度-
print("\nOur IntervalArray length...\n",array.length)
示例
以下是代码-
import pandas as pd #CreatetwoIntervalobjects # Closed intervals set using the "closed" 带值的参数 "both" interval1 = pd.Interval(50, 75, closed='both') interval2 = pd.Interval(65, 95, closed='both') #displaytheintervals print("Interval1...\n",interval1) print("Interval2...\n",interval2) #displaytheintervallength print("\nInterval1 length...\n",interval1.length) print("\nInterval2 length...\n",interval2.length) #ConstructanewIntervalArrayfromIntervalobjects array = pd.arrays.IntervalArray([interval1,interval2]) #DisplaytheIntervalArray print("\nOur IntervalArray...\n",array) #GettingthelengthofIntervalArray #ReturnsanIndexwithentriesdenotingthelengthofeachIntervalintheIntervalArray print("\nOur IntervalArray length...\n",array.length)输出结果
这将产生以下代码-
Interval1... [50, 75] Interval2... [65, 95] Interval1 length... 25 Interval2 length... 30 Our IntervalArray... <IntervalArray> [[50, 75], [65, 95]] Length: 2, dtype: interval[int64, both] Our IntervalArray length... Int64Index([25, 30], dtype='int64')