檢查 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]
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP