Python Pandas - 獲取間隔的長度


若要獲取間隔的長度,請使用 interval.length 屬性。首先,匯入必需的庫 -

import pandas as pd

使用值"neither"的"closed"引數設定開放區間。開放區間(在數學中用方括號表示)不包含其端點,即開放區間 [0, 5] 的特徵是條件 0 < x < 5

interval = pd.Interval(5, 20, closed='neither')

顯示間隔長度

print("\nInterval length...\n",interval.length)

示例

以下為程式碼

import pandas as pd

# Open interval set using the "closed" parameter with value "neither"
# An open interval (in mathematics denoted by square brackets) does not contains its endpoints,
# i.e. the open interval [0, 5] is characterized by the conditions 0 < x < 5.
interval = pd.Interval(5, 20, closed='neither')

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

# the left bound
print("\nThe left bound for the Interval...\n",interval.left)

# the right bound
print("\nThe right bound for the Interval...\n",interval.right)

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

輸出

將生成以下程式碼

Interval...
(5, 20)

The left bound for the Interval...
5

The right bound for the Interval...
20

Interval length...
15

更新於: 2021 年 10 月 20 日

294 次瀏覽

開啟你的職業生涯

透過完成課程獲得認證

開始學習
廣告