Python Pandas - 檢查區間在右側是否閉合


若要檢查某區間是否在左側閉合,請使用 interval.closed_right 屬性。首先,匯入所需的庫 -

import pandas as pd

使用值 "right" 的 "closed" 引數設定區間,即 [0, 5) 採用閉合 "right" 的表示形式 0 < x <= 5

interval = pd.Interval(left=0, right=20, closed='right')

顯示區間

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

檢查該區間是否在右側閉合

print("\nChecking whether the Interval is closed on the right...\n", interval.closed_right)

示例

以下是程式碼

import pandas as pd

# Interval set using the "closed" parameter with value "right"
# i.e. [0, 5) is described by 0 < x <= 5 when closed='right'
interval = pd.Interval(left=0, right=20, closed='right')

# display the interval
print("Interval...\n",interval)

# display the interval length
print("\nInterval length...\n",interval.length)

# check whether the interval is closed on the right-side
print("\nChecking whether the Interval is closed on the right...\n", interval.closed_right)

# check for the existence of an element in an Interval
# This shows that closed = right contain only the right-most endpoint
print("\nThe left-most element exists in the Interval? = \n",0 in interval)
print("\nThe right-most element exists in the Interval? = \n",20 in interval)

輸出

這將生成以下程式碼

Interval...
(0, 20]

Interval length...
20

Checking whether the Interval is closed on the right...
True

更新於: 20-Oct-2021

146 次瀏覽

啟動你的 職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.