Python Pandas - 檢查區間是否在左側、右側、兩側或均未閉合


要檢查區間是否在左側、右側、兩側或均未閉合,請使用 interval.closed 屬性。

首先,匯入所需的庫:

import pandas as pd

使用值為“both”的“closed”引數設定閉區間。閉區間(在數學中用方括號表示)包含其端點,例如閉區間 [0, 5] 的特徵是 0 <= x <= 5

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

顯示區間

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

檢查區間是否在左側、右側、兩側或均未閉合

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

示例

以下是程式碼:

import pandas as pd

# Closed interval set using the "closed" parameter with value "both"
# A closed interval (in mathematics denoted by square brackets) contains its endpoints,
# i.e. the closed interval [0, 5] is characterized by the conditions 0 <= x <= 5.
interval = pd.Interval(left=0, right=20, closed='both')

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

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

# check for the existence of an element in an Interval
# This shows that closed = both contains its endpoints
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]

Checking for the type of Interval...
both

更新於:2021年10月20日

123 次瀏覽

開啟你的職業生涯

完成課程獲得認證

開始學習
廣告