Python Pandas - 将IntervalArray中每个区间的中间点作为索引返回
要将IntervalArray中每个区间的中间点作为索引返回,可以使用**array.mid**属性。首先,
首先,导入所需的库:
import pandas as pd
创建两个区间对象。使用值为“both”的“closed”参数设置闭区间:
interval1 = pd.Interval(50, 75, closed='both') interval2 = pd.Interval(65, 90, closed='both')
显示区间:
print("Interval1...\n",interval1) print("Interval2...\n",interval2)
从区间对象构造一个新的IntervalArray:
array = pd.arrays.IntervalArray([interval1,interval2])
将IntervalArray中每个区间的中间点作为索引:
print("\nThe midpoint of each interval in the IntervalArray...\n",array.mid)
示例
以下是代码:
import pandas as pd # Create two Interval objects # Closed intervals set using the "closed" parameter with value "both" interval1 = pd.Interval(50, 75, closed='both') interval2 = pd.Interval(65, 90, closed='both') # display the intervals print("Interval1...\n",interval1) print("Interval2...\n",interval2) # Construct a new IntervalArray from Interval objects array = pd.arrays.IntervalArray([interval1,interval2]) # Display the IntervalArray print("\nOur IntervalArray...\n",array) # Getting the length of IntervalArray # Returns an Index with entries denoting the length of each Interval in the IntervalArray print("\nOur IntervalArray length...\n",array.length) # midpoint of each Interval in the IntervalArray as an Index print("\nThe midpoint of each interval in the IntervalArray...\n",array.mid)
输出
这将产生以下代码:
Interval1... [50, 75] Interval2... [65, 90] Our IntervalArray... <IntervalArray> [[50, 75], [65, 90]] Length: 2, dtype: interval[int64, both] Our IntervalArray length... Int64Index([25, 25], dtype='int64') The midpoint of each interval in the IntervalArray... Float64Index([62.5, 77.5], dtype='float64')
广告