Python Pandas - 从 IntervalIndex 获取长度
要从IntervalIndex获取长度,请使用Pandas中的interval.length属性。首先,导入所需的库-
import pandas as pd
创建间隔索引-
interval = pd.IntervalIndex.from_arrays([10, 15, 20], [20, 25, 30])
显示间隔-
print("IntervalIndex...\n",interval)
显示间隔长度-
print("\nIntervalIndex length...\n",interval.length)
示例
以下是代码-
import pandas as pd #创建间隔索引 interval = pd.IntervalIndex.from_arrays([10, 15, 20], [20, 25, 30]) #显示间隔 print("IntervalIndex...\n",interval) #显示间隔 length print("\nIntervalIndex length...\n",interval.length) #返回区间的中点 print("\nThe midpoint for the Interval...\n",interval.mid)输出结果
这将产生以下输出-
IntervalIndex... IntervalIndex([(10, 20], (15, 25], (20, 30]], dtype='interval[int64, right]') IntervalIndex length... Int64Index([10, 10, 10], dtype='int64') The midpoint for the Interval... Float64Index([15.0, 20.0, 25.0], dtype='float64')