Python Pandas - 檢查元素是否屬於一個 Interval


要檢查某個元素是否屬於某個 Interval,請使用 in 屬性。首先,匯入所需的庫 −

import pandas as pd

建立時間間隔

interval = pd.Interval(left=0, right=10)

顯示時間間隔

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

檢查某個元素在某個 Interval 中是否存在

print("\nThe specific element exists in the Interval? = \n",6 in interval)

示例

以下是程式碼

import pandas as pd

# Create a time interval
interval = pd.Interval(left=0, right=10)

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

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

# check for the existence of an element in an Interval
print("\nThe specific element exists in the Interval? = \n",6 in interval)

輸出

將生成以下程式碼

Interval...
(0, 10]

Interval length...
10

The specific element exists in the Interval? =
True

更新於: 2021-10-20

2K+ 瀏覽量

啟動您的職業生涯

透過完成課程獲得認證

著手
廣告
© . All rights reserved.