Python Pandas - 从 IntervalIndex 获取中点


要从 IntervalIndex 获取中点,请在 Pandas 中使用 interval.mid 属性。首先,导入所需的库 −

import pandas as pd

创建 IntervalIndex −

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

显示间隔 −

print("IntervalIndex...\n",interval)

返回间隔的中点 −

print("\nThe midpoint for the Interval...\n",interval.mid)

示例

以下为代码 −

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)

# Check whether the IntervalIndex is closed on the left-side, right-side, both or neither
print("\nChecking for the type of IntervalIndex...\n",interval.closed)

# the left bound
print("\nThe left bound for the IntervalIndex...\n",interval.left)

# the right bound
print("\nThe right bound for the IntervalIndex...\n",interval.right)

# 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')

Checking for the type of IntervalIndex...
right

The left bound for the IntervalIndex...
Int64Index([10, 15, 20], dtype='int64')

The right bound for the IntervalIndex...
Int64Index([20, 25, 30], dtype='int64')

The midpoint for the Interval...
Float64Index([15.0, 20.0, 25.0], dtype='float64')

更新于: 18-Oct-2021

363 次浏览

开启你的 职业生涯

通过完成本课程获得认证

开始
广告
© . All rights reserved.