Python Pandas - 从元组数组构造 IntervalArray


要从元组数组构造 IntervalArray,请使用 **pandas.arrays.IntervalArray.from_tuples()** 方法。

首先,导入所需的库 -

import pandas as pd

从元组数组构造新的 IntervalArray: -

array = pd.arrays.IntervalArray.from_tuples([(10, 25),(15, 70)])

显示 intervalArray -

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

获取 IntervalArray 的长度 -

print("\nOur IntervalArray length...\n",array.length)

示例

以下是代码 -

import pandas as pd

# Construct a new IntervalArray from an array-like of tuples
array = pd.arrays.IntervalArray.from_tuples([(10, 25),(15, 70)])

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

输出

这将生成以下代码 -

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

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

The midpoint of each interval in the IntervalArray...
Float64Index([17.5, 42.5], dtype='float64')

更新于: 2021-10-12

89 次浏览

开启你的 职业生涯

通过完成课程获得认证

开始学习
广告