Python Pandas - 获取 IntervalIndex 的长度


要从 IntervalIndex 中获取长度,请在 Pandas 中使用 **interval.length** 属性。首先,导入所需的库 -

import pandas as pd

创建 IntervalIndex -

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

# Create IntervalIndex
interval = pd.IntervalIndex.from_arrays([10, 15, 20], [20, 25, 30])

# Display the interval
print("IntervalIndex...\n",interval)

# Display the interval length
print("\nIntervalIndex length...\n",interval.length)

# return the midpoint of the Interval
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')

更新于: 18-Oct-2021

151 浏览量

职业生涯的起点

完成课程即可获得认证

开始
广告