Python Pandas - 从分割数组构建 IntervalArray 并返回每个区间的左端点


要从分割数组构建 IntervalArray,请使用 **pandas.arrays.IntervalArray.from_breaks()**。要返回每个区间的左端点,请使用 **array.left** 属性。

首先,导入所需的库:

import pandas as pd

从类数组的分割构建新的 IntervalArray:

array = pd.arrays.IntervalArray.from_breaks([0, 1, 2, 3, 4, 5])

显示区间:

print("Our IntervalArray...\n",array)

获取左端点:

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

示例

以下是代码:

import pandas as pd

# Construct a new IntervalArray from an array-like of splits
array = pd.arrays.IntervalArray.from_breaks([0, 1, 2, 3, 4, 5])

# Display the IntervalArray
print("Our 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)

# midpoint of each Interval in the IntervalArray as an Index
print("\nThe midpoint of each interval in the IntervalArray...\n",array.mid)

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

输出

这将产生以下代码:

Our IntervalArray...
<IntervalArray>
[(0, 1], (1, 2], (2, 3], (3, 4], (4, 5]]
Length: 5, dtype: interval[int64, right]

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

The midpoint of each interval in the IntervalArray...
Float64Index([0.5, 1.5, 2.5, 3.5, 4.5], dtype='float64')

The left endpoints of each Interval in the IntervalArray as an Index...
Int64Index([0, 1, 2, 3, 4], dtype='int64')

更新于:2021年10月13日

97 次浏览

启动您的职业生涯

完成课程获得认证

开始学习
广告