Python Pandas - 检查 IntervalIndex 区间是否在左侧、右侧、两侧或都不闭合


要检查 IntervalIndex 区间是否在左侧、右侧、两侧或都不闭合,请使用 **interval.closed** 属性。

首先,导入所需的库 -

import pandas as pd

创建 IntervalIndex -

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

显示区间 -

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

检查 IntervalIndex 是否在左侧、右侧、两侧或都不闭合 -

print("\nChecking for the type of IntervalIndex...\n",interval.closed)

示例

以下是代码 -

import pandas as pd

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

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

输出

这将产生以下输出 -

IntervalIndex...
IntervalIndex([(5, 15], (10, 20], (15, 25]], dtype='interval[int64, right]')

IntervalIndex length...
Int64Index([10, 10, 10], dtype='int64')

Checking for the type of IntervalIndex...
Right

更新于: 2021-10-18

113 次查看

开启你的 职业生涯

通过完成课程获得认证

开始学习
广告