Python Pandas - 返回間隔的中點


要返回間隔的中點,請使用 interval.mid 屬性。首先,匯入所需的庫 -

import pandas as pd

使用值“neither”的“closed”引數開啟間隔集。開放間隔(數學中表示為方括號)不包含其端點,即開放間隔 [0, 5] 的特徵是條件 0 < x < 5

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

顯示間隔

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

返回間隔的中點

print("\nThe midpoint for the Interval...\n",interval.mid)

示例

以下是程式碼

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)

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

# 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)

# return the midpoint of the Interval
print("\nThe midpoint for the Interval...\n",interval.mid)

輸出

這將生成以下程式碼

Interval...
(5, 20)

Interval length...
15

The left bound for the Interval...
5

The right bound for the Interval...
20

The midpoint for the Interval...
12.5

更新於: 21-Oct-2021

589 次瀏覽

啟動您的 職業

完成課程以獲得認證

開始
廣告
© . All rights reserved.