Python Pandas - 将 IntervalArray 中每个 Interval 的右端点返回为一个 Index


要将 IntervalArray 中每个 Interval 的右端点返回为一个 Index,请使用**array.right** 属性。

首先,导入所需的库 −

import pandas as pd

创建两个 Interval 对象 −

interval1 = pd.Interval(10, 25)
interval2 = pd.Interval(15, 70)

显示这些区间 −

print("Interval1...\n",interval1)
print("Interval2...\n",interval2)

从 Interval 对象构造一个新的 IntervalArray −

array = pd.arrays.IntervalArray([interval1,interval2])

获取右端点 −

print("\nThe right endpoints of each Interval in the IntervalArray as an Index...\n",array.right)

示例

以下为代码 −

import pandas as pd
# Create two Interval objects
interval1 = pd.Interval(10, 25)
interval2 = pd.Interval(15, 70)

# display the intervals
print("Interval1...\n",interval1)
print("Interval2...\n",interval2)

# Construct a new IntervalArray from Interval objects
array = pd.arrays.IntervalArray([interval1,interval2])

# Display the IntervalArray
print("\nOur IntervalArray...\n",array)

# Getting the length of IntervalArray
# Returns an Index with entries denoting the length of each Interval in the IntervalArray
print("\nOur IntervalArray length...\n",array.length)

# get the right endpoints
print("\nThe right endpoints of each Interval in the IntervalArray as an Index...\n", array.right)

输出

这将生成以下代码 −

Interval1...
(10, 25]
Interval2...
(15, 70]

Our IntervalArray...
<IntervalArray>
[(10, 25], (15, 70]]
Length: 2, dtype: interval[int64, right]

Our IntervalArray length...
Int64Index([15, 55], dtype='int64')

The right endpoints of each Interval in the IntervalArray as an Index...
Int64Index([25, 70], dtype='int64')

更新时间:2021-10-12

164 个浏览量

开启你的 职业生涯

通过完成课程获取证书

开始
广告