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 次浏览

开启你的职业生涯

通过完成课程获得认证

开始学习
广告