检查 Python Pandas 的 IntervalIndex 中的区间是否逐个元素包含该值
如需返回与当前相同的 IntervalArray,但封闭在指定的一侧,请使用 set_closed() 方法,其参数设置为 both。
首先,导入所需的库 −
import pandas as pd
创建 IntervalArray −
index = pd.arrays.IntervalArray.from_breaks(range(6))
显示区间 −
print("IntervalIndex...\n",index)
返回与当前相同的 IntervalArray,但封闭在指定的一侧,即此处为“both” −
print("\nResult...",index.set_closed('both'))
示例
以下为代码 −
import pandas as pd # Create IntervalArray index = pd.arrays.IntervalArray.from_breaks(range(6)) # Display the interval print("IntervalIndex...\n",index) # Display the interval length print("\nIntervalIndex length...\n",index.length) # the left bound print("\nThe left bound for the IntervalIndex...\n",index.left) # the right bound print("\nThe right bound for the IntervalIndex...\n",index.right) # Return an IntervalArray identical to the current one but closed on specified # side i.e. "both" here print("\nResult...",index.set_closed('both'))
输出
这将产生以下输出 −
IntervalIndex... <IntervalArray> [(0, 1], (1, 2], (2, 3], (3, 4], (4, 5]] Length: 5, dtype: interval[int64, right] IntervalIndex length... Int64Index([1, 1, 1, 1, 1], dtype='int64') The left bound for the IntervalIndex... Int64Index([0, 1, 2, 3, 4], dtype='int64') The right bound for the IntervalIndex... Int64Index([1, 2, 3, 4, 5], dtype='int64') Result... <IntervalArray> [[0, 1], [1, 2], [2, 3], [3, 4], [4, 5]] Length: 5, dtype: interval[int64, both]
广告